note 52720 deleted from function.rand by felipe

From: Date: Sun, 30 Dec 2007 18:43:09 +0000
Subject: note 52720 deleted from function.rand by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-134417@lists.php.net to get a copy of this message
Note Submitter: Gavin ---- There is one problem with Luis and Goochivasquez's code. substr starts at 0 not one, so the string is 0-39 not 1-40. This means that substr would return false sometimes and would give you a length smaller than expected. Here's a fix: // RANDOM KEY PARAMETERS $keychars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $length = 40; // RANDOM KEY GENERATOR $randkey = ""; $max=strlen($keychars)-1; for ($i=0;$i<$length;$i++) { $randkey .= substr($keychars, rand(0, $max), 1); }

« previous php.notes (#134417) next »