Random String Generator

This PHP function generate a random string of a particular length and is very useful to create random passwords.

Random String Generator
<?php

// generate a random string of a particular length
function random_string($length) {
	$string = "";

	// random a string with at least $length characters
	for ($i = 0; $i <= ($length/32); $i++)
		$string .= md5(time() + rand(0, 99));

	// start point limit
	$max_start_index = (32*$i)-$length;

	// take the string
	$random_string = substr($string, rand(0, $max_start_index), $length);

	return $random_string;
}

?>


To use the function:

<?php

// Gets a random string of length 24
$random_string = random_string(24);

?>

Publication details

Category: PHP

Published by: loryzz on date: 17.03.2008 21:07:15

Last edit by: loryzz on date: 02.04.2008 16:54:26

Statistics

Details  This article has been viewed 1.331 times.

Send to

add to digg add to reddit add to furl add to facebook add to delicious add to google add to stumbleupon add to technorati add to ask add to live

Ratings

  • Currently 0; ?>/ TOTALSTARS
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Details 0 user comments, 0 still waiting.

Leave a comment

All comments must be approved by site administrator.
Please write comments on topic. Spam will be never approved.


Confirmation code

Please take a look to image and insert character into the form field.

PHP: Linked articles