Create Random Alphanumeric Password in PHP

We can use this simple password generation function to create random passwords. The maximum limit of generated password is 36 characters.

<?php
	function gen_password($size = 8) {
		$size = $size > 36 ? 30 : $size;
		$pool = array_merge(range(0, 9), range('A', 'Z'));
		$rand_keys = array_rand($pool, $size);
 
		$password = '';
 
		foreach ($rand_keys as $key) {
			$password .= $pool[$key];
		}
 
		return $password;
	}
 
	$password = gen_password(10);
?>

We can use it like this.

<?php
	$password = gen_password(10);
?>

,


  1. #1 by Calgary web design - January 16th, 2010 at 16:04

    Thank’s to spend your time to share. This is what I (and others, I believe), really need. It’s really very informative post. Please keep it up. Looking for more relevant post.

  2. #2 by Bijayani - February 2nd, 2010 at 18:55

    Hi,

    I happened to see your post find it quite informative. I would like to share a link where a software engineer has shared a tip on “Alphanumeric Random value”. Here the developer has explained “how to auto generate random alphanumeric value to be used as temporary password”. I am sharing it just for the knowledge purpose.

    Here is the link:
    http://www.mindfiresolutions.com/PHP–Alphanumeric-Random-value-262.php

    Hope you find it useful and of assistance.

    Thanks,
    Bijayani

(will not be published)


Submit Comment
Subscribe to comments feed
  1. No trackbacks yet.