PHP Random String Generator a-z 0-9 function

Jan 27

Lets face it sometimes there’s just a need to have a random string for whatever purpose usually an initial password if you want to create a verification method for your site, usually a randomly generated password someone has to input first is a good means. Its one of the easier ones I know that. But whatever the case, its needed now and again. Most methods Ive come across however for this purpose don’t work well. One that I found recently via search actually broke more than it didn’t when I bench-marked it through a simple foreach loop. Which forced me to rewrite a part of it at which point the breaking didn’t occur so here it is.

function randomString($howbig)
	{
 
		$chars = "abcdefghijkmnopqrstuvwxyz023456789"; 
		srand((double)microtime()*1000000); 
		$i = 0; 
		$pass = ''; 
 
		while ($i <= $howbig)
		{ 
			$num = rand() % 33; 
			$tmp = substr($chars, $num, 1); 
			$pass = $pass . $tmp; 
			$i++; 
		} 
 
	return $pass; 
	}

Related posts:

  1. PHP mail() with HTML output in a Simple to re-use function

Leave a Reply

*