I’m currently facing an issue with profile image access in my CodeIgniter 4 (ci4) application. After a user logs in, they can access the "Match Profile" and "View Profile" pages, and this functionality is working as expected. However, there’s a problem: if someone shares a direct URL to a profile image, it can be viewed without logging in.
I want to ensure that profile images cannot be viewed without being logged in.
2
Answers
You can store your images outside the public directory which can be accessed by anyone and serve them using a controller in which you can check if the user is logged in, this is an example of a controller you can create:
You’ll also need to create a folder named "private" in the root of your project where you can store the images and you’ll need to add the route to your Routes.php file.
Credits to this answer for the code i used to return the image.