/    Sign up×
Community /Pin to ProfileBookmark

Need security for a random.php file

I’ve got a basic random.php file that displays a random picture on my frontpage (code is displayed below). I’ve recently noticed that someone posted a link in a forum directly to the random.php file that bypasses my frontpage like this [url]http://www.mysite.com/random.php[/url]. All of the pics display on that page but of course I want people to only see these random pics on my frontpage. Anyone know how I can accomplish this?

Also, I’d like to provide hotlink protection for my pics. People have also been posting that same link – ‘http://www.mysite.com/random.php‘ in their posts in forums so that every time someone views that topic in the forum they’ll see a random pic straight from my site. I’ve tried using an htaccess hotlink script that blocks image files but since my images are are coming from the random.php file it doesn’t work unless I add .php to the list of image files. But when I do that nothing shows up on my front page. I hope this hasn’t been too convoluted for you. I’m definitely new to this stuff.

[code]<?php
Header(“Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0”);
Header(“Expires: Thu, 19 Nov 1981 08:52:00 GMT”);
Header(“Pragma: no-cache”);
Header(“Content-Type: image/jpg”);

$dir = “images”; // This is the folder where the images are

srand((double)microtime()*1000000);
$i = 0;
$dirHandle = opendir($dir); // Open the images folder
while(($im = readdir($dirHandle)))
{
if($im != “..” && $im != “.”) // Don’t read in the 2 folders “..” and “.”
{
$image[$i] = $im; // Select an image
$i++;
}
}
closedir($dirHandle); // Close the folder
$n = rand(0,(count($image)-1));

if(!readfile($dir.”/”.$image[$n])) // Read the image
readfile($dir.”error/error.gif”); // If the script can’t find the directory, display this image
?>[/code]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @bluediamond spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...