Forum Moderators: coopster

Message Too Old, No Replies

Generate random number

How can I generate a random number to store?

         

ed_edin

12:10 pm on Jul 16, 2004 (gmt 0)

10+ Year Member



Hi

I have a PHP survey across multiple pages, and have been using people's e-mail addresses to identify them and put the different mysql tables together later. However, if two people decide not to provide an e-mail address I don't know which responses go with which person.

Is there a way I can generate a unique random number which I can hide in each form across the pages?

Many thanks

Ed

Netizen

12:47 pm on Jul 16, 2004 (gmt 0)

10+ Year Member



Try uniqid [php.net] - the PHP manual has a good example.

Receptional

12:48 pm on Jul 16, 2004 (gmt 0)



When I was at college, I was told to do it based on the computer's clock, using .00000000XXXX of a second where X is the random number.

ed_edin

1:05 pm on Jul 16, 2004 (gmt 0)

10+ Year Member



Receptional: that sounds useful. How would this look in a PHP script? I'm a novice.

Nutter

10:06 pm on Jul 16, 2004 (gmt 0)

10+ Year Member



I'm going to second the suggestion for UniqID(). The way I usually use it is to append the IP address of the visitor onto the front and strip out periods.

$var = str_replace('.', '', uniqid($_SERVER['REMOTE_ADDR'], TRUE)); 

Depending on how long the IP address is this will return a string up to 35 characters long that is based on microseconds. If two users happen to click the link at the same microsecond (not too likely) they will have a different IP address tagged onto the front.

- Ryan

ed_edin

1:39 pm on Jul 26, 2004 (gmt 0)

10+ Year Member



What if they are using IP blocking software?

dreamcatcher

9:51 pm on Jul 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is the code I use for generating random numbers, dunno if its any use to you.


$random= "";

srand((double)microtime()*1000000);

$block = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$block .= "0123456789";

for($i = 0; $i < 10; $i++)
{
$random .= substr($block,(rand()%(strlen($block))), 1);
}

echo $random;

This will (or should) display something like: A6GF85CS3L