Random images in PHP?

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

kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Random images in PHP?

Post by kdidymus »

I've set up a security feature on my website which requires a visitor to enter an Anti-Spam code in order to reveal my e-mail address.

This can be seen and tested at:

http://www.didymus.org.uk/contact.php

The form sends the input to my reveal.php page which checks the input against a MySQL database and, if the code is correct, reveals my e-mail address. If it's wrong it produces an error page.

Okay. So that works. But at the moment I'm stuck with one image and one entry in my MySQL table.

Is there a way of randomly loading an image from a bank of perhaps 5 - 10? Preferably without using Javascript?

Thanks in advance.
User avatar
Frozenlight777
Forum Commoner
Posts: 75
Joined: Wed May 28, 2008 12:59 pm

Re: Random images in PHP?

Post by Frozenlight777 »

I think checking out this tutorial might help. That's what I used to figure out how CAPTCHA worked and the randomization of text. http://www.codewalkers.com/c/a/Miscella ... -with-PHP/

Hope that helps a little bit.
User avatar
JamesRavenscroft
Forum Newbie
Posts: 10
Joined: Thu Jan 31, 2008 3:45 am
Location: West Midlands, United Kingdom

Re: Random images in PHP?

Post by JamesRavenscroft »

To load an image out of 5:

Code: Select all

 
<?php
$i = rand(1,5); //take a random number between 1 and 5
 
print "<img src='random_image_$i.jpg' alt='random image'>"; //prints an image tag with the random number factored in. Now just call all your images random_image_1.jpg, random_image_2.jpg etc...
?>
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Re: Random images in PHP?

Post by Jaxolotl »

Tell me if it's useful for you

The only thing you have to do is to set up the directory and just upload your files when you need it.
You may add some extra code to check if file is really an image file

Code: Select all

 
<?php
$my_array = Array();
$my_dir = "myDir/";   //Replace it with your preferred file directory (don't delete the slash)
    if ($dir = @opendir("$my_dir")) {
        
          while (($file = readdir($dir)) !== false) { 
               if ($file != "." && $file != ".."  && !is_dir($my_dir.$file)) 
                       {
                       /* echo $my_dir.$file."<br>";  DEBUG show file list */
                       $my_array[] = $file;
                       
                       } 
          }  
      closedir($dir);
    }
 
$random_number=rand(0, count($my_array)-1); 
$random_file=$my_array[$random_number];
 
 
 
This code show a random image
 echo "<img src=\"".$my_dir.$random_file."\" border=\"0\" alt=\"\">";
 
//This code includes the text file file
//include($my_dir.$random_file);
?>
 
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Random images in PHP?

Post by kdidymus »

Thank you all. Been playing with my site to perfect the way in which my actual code works. Will be messing around with your suggestions tomorrow morning and will let you know if I encounter any problems.

Kris.
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Random images in PHP?

Post by kdidymus »

Thank you all once again. James, I used your code as it was the simplest and quickest to implement and as you will see if you visit my site again, it's working fine.

I'm very grateful to you all for your advice and help.

KD.
helraizer
Forum Commoner
Posts: 31
Joined: Thu Jun 05, 2008 8:20 pm

Re: Random images in PHP?

Post by helraizer »

kdidymus wrote:Thank you all once again. James, I used your code as it was the simplest and quickest to implement and as you will see if you visit my site again, it's working fine.

I'm very grateful to you all for your advice and help.

KD.
Is the image always the same depending on the random number? So is image5 always the same or is image2 the same everytime 5 or 2 come up as the random number?

If that's the case then it's not as secure as it can be becasue the bot could just keep trying and trying since there are only 5 options. With the random codes there is a virtually infinite (worth speaking of) number of codes.

Sam
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Random images in PHP?

Post by kdidymus »

You're quite right about that and I've looked in to other ways of producing a CAPTCHA image but they're all WAY too complicated for me!

I have ten images which load randomly. I guess there IS a chance that a bot could crack this but I don't know in all honesty how likely that is.

The reason I opted for this option is it's something I know. I figured create a table with the possible codes and then check the input against all ten possibles. If ONE of them matches, then allow the user to continue.

And that was the basis of my idea. Because the first page sends to PHP and doesn't actually contain any detail, it's only by guessing the number correctly that (having checked the input against the database) the form re-directs the user to the actual page containing my e-mail address (which is stored in the MySQL database alongside each of the possible correct answers).

Is there a way of testing whether a site is "bot proof"?
helraizer
Forum Commoner
Posts: 31
Joined: Thu Jun 05, 2008 8:20 pm

Re: Random images in PHP?

Post by helraizer »

kdidymus wrote: I figured create a table with the possible codes and then check the input against all ten possibles. If ONE of them matches, then allow the user to continue.
Does that mean you could always use the same code, because it'd match; even if the image changes?

Something like :

Code: Select all

 
<?php
 
function assign_rand_value($num)
{
    // accepts 1 - 36
    switch ($num) {
        case "1":
            $rand_value = "a";
            break;
        case "2":
            $rand_value = "b";
            break;
        case "3":
            $rand_value = "c";
            break;
        case "4":
            $rand_value = "d";
            break;
        case "5":
            $rand_value = "e";
            break;
        case "6":
            $rand_value = "f";
            break;
        case "7":
            $rand_value = "g";
            break;
        case "8":
            $rand_value = "h";
            break;
        case "9":
            $rand_value = "i";
            break;
        case "10":
            $rand_value = "j";
            break;
        case "11":
            $rand_value = "k";
            break;
        case "12":
            $rand_value = "l";
            break;
        case "13":
            $rand_value = "m";
            break;
        case "14":
            $rand_value = "n";
            break;
        case "15":
            $rand_value = "o";
            break;
        case "16":
            $rand_value = "p";
            break;
        case "17":
            $rand_value = "q";
            break;
        case "18":
            $rand_value = "r";
            break;
        case "19":
            $rand_value = "s";
            break;
        case "20":
            $rand_value = "t";
            break;
        case "21":
            $rand_value = "u";
            break;
        case "22":
            $rand_value = "v";
            break;
        case "23":
            $rand_value = "w";
            break;
        case "24":
            $rand_value = "x";
            break;
        case "25":
            $rand_value = "y";
            break;
        case "26":
            $rand_value = "z";
            break;
        case "27":
            $rand_value = "0";
            break;
        case "28":
            $rand_value = "1";
            break;
        case "29":
            $rand_value = "2";
            break;
        case "30":
            $rand_value = "3";
            break;
        case "31":
            $rand_value = "4";
            break;
        case "32":
            $rand_value = "5";
            break;
        case "33":
            $rand_value = "6";
            break;
        case "34":
            $rand_value = "7";
            break;
        case "35":
            $rand_value = "8";
            break;
        case "36":
            $rand_value = "9";
            break;
    }
    return $rand_value;
}
 
function get_rand_id($length)
{
    if ($length > 0) {
        $rand_id = "";
        for ($i = 1; $i <= $length; $i++) {
            mt_srand((double)microtime() * 1000000);
            $num = mt_rand(1, 36);
            $rand_id .= assign_rand_value($num);
        }
    }
    return $rand_id;
}
 
$code = get_rand_id(6);
 
$_SESSION['code'] = $code;
 
 
$width = 300;
$height = 100;
$image = imagecreatetruecolor(300, 100);
$grey = imagecolorallocate($image, 180, 180, 180);
$black = imagecolorallocate($image, 0, 0, 0);
$red = imagecolorallocate($image, 200, 0, 0);
$noise = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, rand(0, 255), mt_rand(0, 100), mt_rand(100, 200));
$color2 = imagecolorallocate($image, rand(0, 255), mt_rand(0, 100), rand(100,
    200));
$color3 = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 100), mt_rand(100,
    200));
$color4 = imagecolorallocate($image, rand(0, 255), mt_rand(0, 100), rand(0,
    200));
 
Imagefill($image, 0, 0, $grey);
 
imagesetthickness($image, 2);
 
for ($i = 0; $i < 800; $i++) {
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $color);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $color2);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $noise);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $color);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $color3);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $noise);
 
    imageellipse($image, $width / 2, $height / 2, $width, $height, $color4);
}
 
 
imagettftext($image, 50, 10, 79, 78, $grey, "Font-On-A-Stick.ttf", $code);
imagettftext($image, 50, 10, 82, 81, $black,
    "Font-On-A-Stick.ttf", $code);
imagettftext($image, 50, 10, 85, 84, $grey, "Font-On-A-Stick.ttf", $code);
imagettftext($image, 50, 10, 88, 87, $black,
    "Font-On-A-Stick.ttf", $code);
 
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
?>
 
produces this:

Image

Sam
Last edited by helraizer on Tue Jun 10, 2008 11:20 am, edited 1 time in total.
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Random images in PHP?

Post by kdidymus »

That's cool. I like that alot.

But how would I now implement that? I mean clearly I'd be scrapping the table in my Database but how do I go about checking the input and returning the second page when it's correct?

Sorry. I'm really thick when it comes to PHP!!

KD.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Random images in PHP?

Post by superdezign »

Save the raw text in the session and check it against the input in the posted input.

Also, a cleaner version of the assign_rand_value() function:

Code: Select all

function assign_rand_value($value)
{
  $validValues = 'abcdefghijklmnopqrstuvwxyz0123456789';
  return $validValues[$value - 1];
}
Of course, you could probably combine that, now, small snippet into the function that calls it easily.
helraizer
Forum Commoner
Posts: 31
Joined: Thu Jun 05, 2008 8:20 pm

Re: Random images in PHP?

Post by helraizer »

kdidymus wrote:That's cool. I like that alot.

But how would I now implement that? I mean clearly I'd be scrapping the table in my Database but how do I go about checking the input and returning the second page when it's correct?

Sorry. I'm really thick when it comes to PHP!!

KD.
On your first page I'd assume you have a field for them to enter the code, like input field; name "code".

On your page 2 (second page):

Code: Select all

 
$code = $_POST['code'];
 
if($code == $_SESSION['code'])) { // code correct
 
// show your normal code for the page - same as your method.
} else // code not correct.
{
die("CAPTCHA code incorrect, please try again. <br><br>  Click <a href='yourpage1.php'>here</a> to go back."); 
}
 

Sam
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Random images in PHP?

Post by tecktalkcm0391 »

Easiest way to get CAPTCHA implemented:

http://recaptcha.net/

They generate everything, and fix security holes... you just set up a few lines of code to check to make sure then entered the right code...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Random images in PHP?

Post by Weirdan »

tecktalkcm0391 wrote:They generate everything, and fix security holes... you just set up a few lines of code to check to make sure then entered the right code...
Except it doesn't work with javascript turned off.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Random images in PHP?

Post by Zoxive »

Weirdan wrote:
tecktalkcm0391 wrote:They generate everything, and fix security holes... you just set up a few lines of code to check to make sure then entered the right code...
Except it doesn't work with javascript turned off.
I haven't messed with it lately, but there use to be a non-javascript version.

Using iframes i believe.
Post Reply