Tuesday, June 13, 2006

PHP Random String Generator function

function random_gen($length)
  $random= "";
  $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:
Bookmark and Share


Blogger Oleg said...

It is helpful. thanks

but there is an error:
$strset variable should be replaced with $char_list

otherwise its fine

Thanks in any case.

June 05, 2008 7:38 AM  
Blogger Giacomo Arru said...

the function is broken,

change $strset with $char_list
and remove $random= ""; (pretty ugly)

June 07, 2008 2:46 PM  
Anonymous Anonymous said...

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 :))

June 12, 2008 4:39 PM  
Blogger yalamber said...

undifined variable $strset

November 30, 2008 8:09 PM  
Anonymous Anonymous said...

Just for anyone else using this, there is a bug in the code. Instead of using the variable '$strset', it should be '$char_list'.

January 16, 2009 2:17 PM  
Anonymous Anonymous said...

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.

January 30, 2009 12:46 AM  
Anonymous Anonymous said...

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.

February 14, 2009 12:40 AM  
Blogger Richard said...

There is an error in this function. the parameter in the substring should be $char_set, not $strset.

February 27, 2009 9:06 AM  
Anonymous Anonymous said...

references to $strset should read $char_list

March 03, 2009 5:13 PM  
Anonymous Anonymous said...

The code is wrong and throws errors. Where you have $strset, it needs to be changed instead to $char_list.

March 27, 2009 9:41 AM  
Anonymous BateRiste said...

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 .= "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;

May 06, 2009 7:25 AM  
Anonymous 911... Need Code, Help said...

How about these similar code examples:

Generate Random Strings Using PHP


Generate Random Strings Using ASP/VBScript

June 26, 2009 1:00 PM  
Anonymous Andrius said...

This script is bad, because he working very slow. This script is better then this:

$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";

$string_length = 10;
$random_string = '';

for($i = 1; $i <= $string_length; $i++)
$rand_number = rand(0, 59);
$random_string .= $chars[$rand_number];

echo $random_string;

August 04, 2011 11:02 PM  

Post a Comment