note 74499 added to function.mt-rand

From: Date: Sat, 14 Apr 2007 04:06:31 +0000
Subject: note 74499 added to function.mt-rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-125374@lists.php.net to get a copy of this message
I noticed something odd happening when I wrote this little function to generate a random code for my captcha system: <?php function generatecaptcha() { $captcha = ""; $possiblechars = "0123456789bcdfghjkmnpqrstvwxz"; //29 chars $loop = 0; while ($loop < 5) { $randomchar = substr($possiblechars, mt_rand(0, 28), 1); if (!strstr($captcha, $randomchar)) { $captcha .= $randomchar; $loop++; } } return $captcha; } ?> It's supposed to generate a random 5 chars long string to be used as a captcha code. I use !strstr to make sure no characters are used twice. The $possiblechars variable contains the chars that are used to create the captcha code. I don't use any vowels or similar letters. I've noticed that, even though !strstr is used, sometimes two zeros will appear on the resulting captcha code, something like "xt00g". And I only noticed that happening with zeros. Could it be a bug on strstr function? Or is my code faulty somewhere I missed? ---- Server IP: 64.71.164.2 Probable Submitter: 24.117.184.248 ---- Manual Page -- http://www.php.net/manual/en/function.mt-rand.php Edit -- https://master.php.net/note/edit/74499 Del: integrated -- https://master.php.net/note/delete/74499/integrated Del: useless -- https://master.php.net/note/delete/74499/useless Del: bad code -- https://master.php.net/note/delete/74499/bad+code Del: spam -- https://master.php.net/note/delete/74499/spam Del: non-english -- https://master.php.net/note/delete/74499/non-english Del: in docs -- https://master.php.net/note/delete/74499/in+docs Del: other reasons-- https://master.php.net/note/delete/74499 Reject -- https://master.php.net/note/reject/74499 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125374) next »