Prashant Ingale

Juice of PHP, MySQL, Apache, Linux, Actionscript, Flex

Random password generator using PHP

leave a comment

Here is simple script for generating random string with numbers and charactors. Length can be decided using parameter passed to function or can be static by setting up in function.

function generatePassword($length = 8) {

		// start with a blank password
		$password = "";

		// define possible characters
		$possible = "0123456789bcdfghjkmnpqrstvwxyz";

		// set up a counter
		$i = 0;

		// add random characters to $password until $length is reached
		while ( $i < $length ) {

			// pick a random character from the possible ones
			$char = substr ( $possible, mt_rand ( 0, strlen ( $possible ) - 1 ), 1 );

			// we don't want this character if it's already in the password
			if (! strstr ( $password, $char )) {
				$password .= $char;
				$i ++;
			}

		}

		// done!
		return $password;

	}

Written by admin

September 12th, 2008 at 1:35 pm

Posted in PHP

Leave a Reply

You must be logged in to post a comment.