Page 1 of 1

Calling Randon Numbers in an Anti Automated Signup Script

Posted: Wed Apr 23, 2003 3:18 am
by leebies
I have a problem with calling a random number.

I have created a page saying:

<img src="images.php"> <img src="images.php"> <img src="images.php"> <img src="images.php">

In theory this will show 4 random numbers as images.php says the following:

<?
header("content-type: image/gif");
session_start();
//$n is randomly selected from 1 - 10
$HTTP_SESSION_VARS['number'] = "'.$HTTP_SESSION_VARS['number'].'".$n;
echo fread(fopen("images/$n.gif","r"),filesize("images/$n.gif"));
exit;
?>

The problem is it does not show any of the images 1.gif etc.

I tried $n = rand (1, 10); but when I did that, it showed the numbers and they are all the same. Has anyone got any ideas?

Cheers

Posted: Wed Apr 23, 2003 6:48 am
by d1223m
what is this meant to do?

Code: Select all

//$n is randomly selected from 1 - 10
$HTTP_SESSION_VARS&#1111;'number']="'.$HTTP_SESSION_VARS&#1111;'number'].'".$n;
at the moment this code wont do anything because $n is not set.

this should work fine $n=rand(1,10);
but take note ( from the php manual) :
[qutoe]
In older versions of PHP, you had to seed the random number generator before use with srand(). Since 4.2.0 this is no longer necessary.
[/quote[

also, better than this is the fpassthru command i think :

Code: Select all

echo fread(fopen("images/$n.gif","r"),filesize("images/$n.gif"));
becomes the more standard:

Code: Select all

$fp = fopen($filename, 'r');
fpassthru($fp); // ** CORRECT **
fclose($fp);

Posted: Wed Apr 23, 2003 8:16 am
by volka
http://www.php.net/manual/en/function.rand.php
In older versions of PHP, you had to seed the random number generator before use with srand(). Since 4.2.0 this is no longer necessary.
may this cause the problem with $n = rand(1,10);?

New version

Posted: Wed Apr 23, 2003 8:49 am
by leebies
I am now doing the following:
<?
header("content-type: image/gif");
session_start();
//$n is randomly selected from 1 - 10
$n=rand(1,9);
$fp = fopen("images/$n.gif", 'r');
fpassthru($fp);
fclose($fp);
exit;
?>
It works like the other code, unfortunately it shows all the numbers the same. http://www.childcarejobs.co.uk/checker is where the script is.

I am stuck as well on how to get the 4 number value to save in the sessions so that when I press submit, the numbers in the box are compared with the number values of the picture, and if they tally, they are taken to the correct page.

Help!

Posted: Wed Apr 23, 2003 9:29 am
by d1223m
i just got the numbers 3,5,6,3

none of them being 4.

you got a cache turned on?

Done!

Posted: Wed Apr 23, 2003 9:35 am
by leebies
Needed to seed srand! Thank you soooo much for the help! :)