02-12-2009

PHP Generate random password with php

This function will generate a random password string. With the length argument you can controll the number of characters returned.

function generatePassword($length = 8)
{	
	return ucfirst(substr(md5(uniqid(rand()),false),0,$length));
}

Comments:

3 comments.
Your comment:

»
Ayush 21/09/2010, 6:29 am
Nice example (:
Temp 06/07/2011, 12:11 pm
From a cryptographic point of view this is terrible.
First letter is always capital, only symbols 0-9 a-f are used...

return substr(base64(mt_rand()),0,$length);
is faster shorter and will do a better job.
( -_-) 27/11/2011, 4:31 pm
base64 is not a native php function

You probably meant:

base64_encode(mt_rand()),0,$length);

 

[x]