note 52720 deleted from function.rand by felipe

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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);
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.