Calling Randon Numbers in an Anti Automated Signup Script

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

Post Reply
leebies
Forum Newbie
Posts: 9
Joined: Wed Apr 23, 2003 3:18 am

Calling Randon Numbers in an Anti Automated Signup Script

Post 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
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post 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);
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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);?
leebies
Forum Newbie
Posts: 9
Joined: Wed Apr 23, 2003 3:18 am

New version

Post 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!
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

i just got the numbers 3,5,6,3

none of them being 4.

you got a cache turned on?
leebies
Forum Newbie
Posts: 9
Joined: Wed Apr 23, 2003 3:18 am

Done!

Post by leebies »

Needed to seed srand! Thank you soooo much for the help! :)
Post Reply