PHP Random String Generator a-z 0-9 function

///PHP Random String Generator a-z 0-9 function

PHP Random String Generator a-z 0-9 function

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.\r\n\r\n
\r\nfunction randomString($howbig)\r\n	{\r\n\r\n		$chars = "abcdefghijkmnopqrstuvwxyz023456789"; \r\n		srand((double)microtime()*1000000); \r\n		$i = 0; \r\n		$pass = ''; \r\n\r\n		while ($i <= $howbig)\r\n		{ \r\n			$num = rand() % 33; \r\n			$tmp = substr($chars, $num, 1); \r\n			$pass = $pass . $tmp; \r\n			$i++; \r\n		} \r\n\r\n	return $pass; \r\n	}\r\n
\r\n\r\n ". randomString(50)."
";\r\necho "Example: 15 Chars > ". randomString(15)."
";\r\necho "Example: 10 Chars > ". randomString(10)."
";\r\necho "Example: 5 Chars > ". randomString(5)."
";\r\n\r\n?>
By |2014-01-05T10:29:16+00:00January 27th, 2012|Code, PHP|0 Comments

About the Author:

Not much to know about me, I'm a 35+ year old coder, geek, gamer..

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: