PHP Random String Generator function
function random_gen($length)
{
$random= "";
srand((double)microtime()*1000000);
$char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$char_list .= "abcdefghijklmnopqrstuvwxyz";
$char_list .= "1234567890";
// Add the special characters to $char_list if needed
for($i = 0; $i < $length; $i++)
{
$random .= substr($char_list,(rand()%(strlen($char_list))), 1);
}
return $random;
}
$random_string = random_gen(10); //This will return a random 10 character string
Share it with others on:
{
$random= "";
srand((double)microtime()*1000000);
$char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$char_list .= "abcdefghijklmnopqrstuvwxyz";
$char_list .= "1234567890";
// Add the special characters to $char_list if needed
for($i = 0; $i < $length; $i++)
{
$random .= substr($char_list,(rand()%(strlen($char_list))), 1);
}
return $random;
}
$random_string = random_gen(10); //This will return a random 10 character string
Share it with others on:
13 Comments:
It is helpful. thanks
but there is an error:
$strset variable should be replaced with $char_list
otherwise its fine
Thanks in any case.
the function is broken,
change $strset with $char_list
and remove $random= ""; (pretty ugly)
Hmm.. I think you were in a rush when you posted this. The biggest flaw that I can see is that you create a variable called $char_list to gather the characters but you use $strset to grab the characters... $strset doesn't exit so I think it was suppose to be $char_list OR you missed out a line somewhere between the $char_list and $strset variable.
Also.. you can do $char_list = "sfsfsf" . "fsddssdf" . "sfsdfsf" without having to keep stating $char_list each line...
Thanks though, it looks pretty cool apart from that fact it doesn't work :))
undifined variable $strset
Just for anyone else using this, there is a bug in the code. Instead of using the variable '$strset', it should be '$char_list'.
You forgot a critical part of the code. You are accessing a variable "$strset", that does not exist. $strset is supposed to be $char_list. Before the for() loop, add $strset = $char_list or replace all $strset occurences with $char_list to get this to work.
It works! But there was one problem. $strset is not given a value before it is called, so I set it eual to $char_list.
There is an error in this function. the parameter in the substring should be $char_set, not $strset.
references to $strset should read $char_list
The code is wrong and throws errors. Where you have $strset, it needs to be changed instead to $char_list.
This code is not working, $strset is not defined. The source code that works for me is as follows:
function generate_password($p_length) {
$char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$char_list .= "abcdefghijklmnopqrstuvwxyz";
$char_list .= "1234567890";
$char_list .= "!@#$?-_";
$random = "";
srand((double) microtime() * 1000000);
for($i = 0; $i < $p_length; $i++) {
$random .= substr($char_list, (rand() % (strlen($char_list))), 1);
}
return $random;
}
How about these similar code examples:
Generate Random Strings Using PHP
and:
Generate Random Strings Using ASP/VBScript
This script is bad, because he working very slow. This script is better then this:
//Chars
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
//Parametres
$string_length = 10;
$random_string = '';
//Gererating
for($i = 1; $i <= $string_length; $i++)
{
$rand_number = rand(0, 59);
$random_string .= $chars[$rand_number];
}
echo $random_string;
Post a Comment