Connecting Tech Pros Worldwide Help | Site Map

Random password generation

  #1  
Old June 16th, 2009, 12:20 PM
Member
 
Join Date: Aug 2008
Posts: 40
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????
  #2  
Old June 16th, 2009, 03:53 PM
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
Provided Answers: 2

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, ... ... ...)")
  #3  
Old June 16th, 2009, 05:18 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,858
Provided Answers: 9

re: Random password generation


You could just use one of the built in random functions or hash functions.
  #4  
Old June 17th, 2009, 06:44 AM
Member
 
Join Date: Aug 2008
Posts: 40

re: Random password generation


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
hello i need help with torque (java )to setup him to db2 yoach answers 0 July 17th, 2008 11:45 AM