note 74499 rejected from function.mt-rand by bjori
| From: | bjori@php.net | Date: | Sat, 14 Apr 2007 09:50:19 +0000 |
| Subject: | note 74499 rejected from function.mt-rand by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125388@lists.php.net to get a copy of this message | ||
Note Submitter: earlsinclair2001 at yahoo dot com
----
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?