note 27132 deleted from function.rand by aidan
| From: | aidan@php.net | Date: | Sat, 10 Jul 2004 02:34:10 +0000 |
| Subject: | note 27132 deleted from function.rand by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72964@lists.php.net to get a copy of this message | ||
Note Submitter: tony@popnotsoda.com
----
A useful thing to do is to create a random user ID that can be stored as a cookie, then used to look
up that user's info in a database. The "tricky" part is making sure it isn't
already used as someone else's ID. Here's one way to do it (I'm sure it could be
optimized, suggestions are welcomed):
do {
$new_id = "";
for ($i = 0; $i < 255; $i++)
$new_id .= chr(rand(97, 122));
$query = "SELECT UserID FROM my_table
WHERE UserID = '$new_id' LIMIT 1";
$id_rows = mysql_query($query);
$num_matching_ids = mysql_num_rows($id_rows);
} while ($num_matching_ids > 0);