Random PAssword Generation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dwfait
Forum Contributor
Posts: 113
Joined: Sun Aug 01, 2004 10:36 pm

Random PAssword Generation

Post by dwfait »

Hi. How would i generate a long radnom password, consisting of numbers and letters?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Code: Select all

<?php
$strList=array();
for ($i=48;$i<=57;$i++) $strList[]=chr($i);
for ($i=65;$i<=90;$i++) $strList[]=chr($i);
for ($i=97;$i<=122;$i++) $strList[]=chr($i);
$Pass="";
for ($i=0;$i<50;$i++) $Pass.=$strList[rand(0,count($strList)-1)];
echo $Pass;
?>
dwfait
Forum Contributor
Posts: 113
Joined: Sun Aug 01, 2004 10:36 pm

Post by dwfait »

Thanks. Also, im saving my passwords as an MD5 value in my database. If one of the users were to lose their passwords, how would i revert their password from MD5 to their normal password to email it to them?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

You cannot get back MD5 values back. Either you have to generate a new password and mail or use mcrypt functions instead where you can encryt and decrypt using your own key.
http://www.php.net/manual/en/ref.mcrypt.php
Post Reply