Securing image or document file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
markthien
Forum Commoner
Posts: 33
Joined: Fri Feb 13, 2009 7:50 pm

Securing image or document file

Post by markthien »

Hi guys,

I need to secure file inside a folder where user need to login first then they can download or view the file. For example, if user directly visit

http://www.ifnoresponse.com/upload/image1.jpg

if she has not login yet, system will redirect her to login page.

Just like if someone send you a file in gmail, you will be able to download or view it if your gmail session still alive. Else if you direct access that file without login to your gmail, gmail will redierct you to login page.

Any advice is appreciated

regards,
Mark Thien
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Securing image or document file

Post by social_experiment »

Placing the images outside the root folder is a good start otherwise those files to be secured can be accessed regardless of any security in the script. On the page that displays the image you will have checks to see if a user is logged in, using you prefered method (cookies, database, session, etc).

Code: Select all

<?php
 // assume session are used
 if (!isset($_SESSION['loggedIn']) || !isset($_SESSION['userId']))
 {
   redirectUser();
 }
?>
This will be at the top off the page that displays the image so when the page is loaded without being logged in, the user gets redirected.

Have a look at this topic, it is about accessing files outside the root folder and displaying them
Accessing files outside the web root
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
markthien
Forum Commoner
Posts: 33
Joined: Fri Feb 13, 2009 7:50 pm

Re: Securing image or document file

Post by markthien »

wow that's really useful info. Thanks a lot mate !! :P :mrgreen:
Post Reply