Random password generation - Php

Random password

Snippet Code


  
Rate this page :
  [ 0 votes]

Using this we can generate random password for every page refresh. We can also set password length.

<?php function generate_password($length, $characters='abcdefghijklmnopqrstuvqxyz1234567890'){ if ($characters == ''){ return ''; } $chars_length = strlen($characters)-1; mt_srand((double)microtime()*1000000); $pwd = ''; while(strlen($pwd) < $length){ $rand_char = mt_rand(0, $chars_length); $pwd .= $characters[$rand_char]; } return $pwd; } //pass the length of the password as argument $res = generate_password(7); echo "<div><input type='text' name='random' value='$res'></div>"; ?>

Tags


Ask Questions

Ask Question