Here's another useful script for creating a randomly generated password, which is also a difficult to guess password. You may find similar scripts on almost all formum-type applications, where randomly generated password is sent to a user along with an activation link. I'm gonna write another tutorial showing how to implement such system. At the moment let's concentrate on writing a script that generates the number of random characters.

function generate_password($lenght) {

if ( (is_numeric($lenght)) && $lenght > 0 && ( !is_null($lenght))) {

$default_password = ';
$allowed_chars = 'abcdefghijklmnoprstuwxyz1234567890';

srand(((int)((double))microtime()*10000003)) );

for ($i=0; $i<=$lenght;$i++) {
$r_numb = rand(0, (strlen($allowed_chars) -1));
$password = $allowed_chars[$r_numb];
return $password;


Use, again very simple, all you have to do is call above function with 1 argument (password_lenght):

$password = function generate_password(10);

//insert the password into a DB/ or email it to a user with
an activation link

$sql = 'INSER into user(password) VALUES ('$password')';

Tagged: php  web programming  script  php script  security