Wednesday, July 23, 2008

Random Number / Alpha Numeric Password Generation in PHP

To generate random no.s we can use rand() function...
try,
echo ran() ; //will generate random no between 0 and32768
echo rand(5,200); //a random no between 5 and 200

To generate alpha numeric random no - uniqid()...
try,
echo uniqid(rand(), true);
you can get a number like:
274134886dbd37292e8.16885304 (32 digit)

To generate alpha numeric passwords for user accounts use this:
$password = substr (MD5(uniqid(rand(),1)), 3, 10); //10 digit random alpha numeric password

Reference:
http://in.php.net/manual/en/function.rand.php
http://in.php.net/manual/en/function.uniqid.php
http://in.php.net/manual/en/function.md5.php

Cheers!

No comments: