tags:

views:

224

answers:

6

Hi, when users upload the image i save it to the image folder in htdocs directory. so any user without logging into site can go type the url/images/"name" would get it. what is the best way to prevent this. since the browse would just request just like the user typing directly in the address bar the location of the image. I was thinking of using a script to download each image file from a directory after checking the session details. Do you think will it will a good approach if so can you suggest me a script. I don't want to use database. I think it will be slow. OR if it is the better approach let me know.

THnks

A: 

Yes you could have a image.php file that just adds the userid at the end and displays the image. Its not that much code actually.

An example is in the readfile() documentation. But that downloads the image, you could just skip the attachment header and use a content-type header.

You could save the image location in a database, that way its not slow.. as I do not recommend storing pure image data in a database

Ólafur Waage
+8  A: 

You could put it outside the htdocs/ directory, and mod_rewrite the images/ dir to image.php or something. So url/images/test.jpg would translate to image.php?path=test.jpg

image.php may look something like this:

<?php
if($loggedin) {
    header("Content-Type: image/jpeg");
    echo file_get_contents("../images/".$_GET["path"]);
}
?>

Don't forget to sanitize the input! You don't want the user to access arbitrary files.

antennen
so when i write my php script to show picture of a person just put the <img src=picturename.jpg > and using mod_rewrite I put I conver the <img src=picturename.jpg> to image.php?=picturename.jpg. Did get it right. I AM NEWBY sorry for dumb question. what about santizing the input...
coool
what tag should i put image.php?path=test.jpg...should it be<img src=image.php?path=test.jpg>
coool
You got it right. You don't have to do mod_rewrite if you don't want to. Then <img src="image.php?path=test.jpg"> is appropriate. When I say sanitize, I mean making sure you don't allow things like image.php?path=/etc/passwd as this would echo its contents to a logged in user. I.e. making sure nothing other but images in the image dir can be included.
antennen
A: 

If you are not using an authentication library I would use $_SESSION to track if the user is logged in, but also create a fingerprint just to make it more difficult to hijack sessions:

$_SESSION['loggedIn'] = 1;

is a bad way to do it since I can pass ?PHPSESSID=933883&loggedIn=1 and basically trick the application.

$_SESSION['userid'] = 100;
$_SESSION['fingerprint'] = jskdskjdjdk48924829dkshjdkshdjkhsdjsd;

'fingerprint' is some MD5 encrypted string with a salt, ex. username+password+"any string you want" that only you know of. You create the fingerprint on login and have a function validate the fingerprint stored in the session around critical code. This is a basic, example and you can make use of better auth modules present in many PHP frameworks like CakePHP when you get more comfortable with PHP.

Nael El Shawwa
A: 

When they are logged in, set a cookie

then in a readimg.php check for the cookie

then the input for readimg.php?i=img.png

have the readimg.php only check a specific folder for images

Jim
A: 

That's what I'm doing here:

http://numetrocontent.co.za/basic_movie_info.php?movieid=0412

The image you see is a low-res version of a file stored on the server, which you can't see. If you're logged in, you can click on the image to download the original file. If you're not, you can't. You can, however, set arbitrary resolutions for the rendering, but that is by choice.

+2  A: 

Instead of using echo file_get_contents(...) I would recommending using fpassthru to avoid having "out of memory" errors that could occur with large files.

shadowhand