Connecting Tech Pros Worldwide Forums | Help | Site Map

Random password generation

Member
 
Join Date: Aug 2008
Posts: 40
#1: Jun 16 '09
I want to generate password randomly and send to mysql database using PHP..i have got the following function for generating password but doesn't know how to incorporate in my registration form and where to call that function and update password field in database..please help me..
Expand|Select|Wrap|Line Numbers
  1.     function generatePassword ( $length = 8 ) {
  2.     // start with a blank password
  3.     $password = “”;
  4.  
  5.     // define possible characters
  6.     $possible = “0123456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ!@#$%^&*”;
  7.  
  8.     // set up a counter
  9.     $i = 0;
  10.  
  11.     // add random characters to $password until $length is reached
  12.     while ($i < $length) {
  13.     // pick a random character from the possible ones
  14.     $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
  15.  
  16.     // we don’t want this character if it’s already in the password
  17.     if (!strstr($password, $char)) {
  18.     $password .= $char;
  19.     $i++;
  20.     }
  21.     }
  22.     // done!
  23.     return $password;
  24.     }
  25.  
where should i call this function generatePassword????

hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,631
#2: Jun 16 '09

re: Random password generation


Keep this function at the bottom of your PHP script, or in some other dedicated .php file for this function and include that PHP script in your present script.

Then use this function like
Expand|Select|Wrap|Line Numbers
  1. $new_password = generatePassword (10); //whatever length
  2. $sql = mysql_query("INSERT INTO ..... .... (`password`, ... ... ...) VALUES ($new_password, ... ... ...)")
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,134
#3: Jun 16 '09

re: Random password generation


You could just use one of the built in random functions or hash functions.
Member
 
Join Date: Aug 2008
Posts: 40
#4: Jun 17 '09

re: Random password generation


thank hsriat..its working.... :)
Reply