note 27132 added to function.rand

From: Date: Sun, 24 Nov 2002 14:59:20 +0000
Subject: note 27132 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-39913@lists.php.net to get a copy of this message
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); -- http://www.php.net/manual/en/function.rand.php http://master.php.net/manage/user-notes.php?action=edit+27132 http://master.php.net/manage/user-notes.php?action=delete+27132 http://master.php.net/manage/user-notes.php?action=reject+27132

« previous php.notes (#39913) next »