Jump to content

Make this little script more secure.


quizzical

Recommended Posts

Hi all. I need a help with a simple script (should be a simple matter for those who know php well)

 

I have this script that makes possible to download images from my website via text links.

 

<?php

// Make sure that the id query is sent, and that the file exists
if (!isset($_GET['id']) || !file_exists('smilies/amore/'.$_GET['id'])) {
    die('No valid image was supplied.');
}

header('Content-type: image/gif');
header('Content-Disposition: attachment; filename="'.basename($_GET['id']).'"'); // tell the browser how to handle the file
echo implode(null, file('amore/'.$_GET['id'])); // provide the image

?>

 

Problem is, I need to make it more secure because some people are using it to download my php sources. Something like restrict its use to certain folders.

 

How can I achieve this?

 

Thanks in advance

 

Link to comment
https://forums.phpfreaks.com/topic/155813-make-this-little-script-more-secure/
Share on other sites

my images are already in folders, you can see this script working here

http://quizzical.altervista.org/smilies.php

http://quizzical.altervista.org/smilies/amoredownloader.php?id=10.gif

The script is off now because people were looking at my source code in other folders.

How can I do a path check on the parameter ID? (I don't know php that much.)

You could do this:

 

<?php

// Make sure that the id query is sent, and that the file exists
if (!isset($_GET['id']) || !file_exists('smilies/amore/'.$_GET['id'])) {
    die('No valid image was supplied.');
}

// Check if the ID ends with '.gif'
if(substr($_GET['id'], -4) !== ".gif") {
    die('This is not an image!');
}

header('Content-type: image/gif');
header('Content-Disposition: attachment; filename="'.basename($_GET['id']).'"'); // tell the browser how to handle the file
echo implode(null, file('amore/'.$_GET['id'])); // provide the image

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.