By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,630 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,630 IT Pros & Developers. It's quick & easy.

Random password generation

P: 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 dont want this character if its 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????
Jun 16 '09 #1
Share this Question
Share on Google+
3 Replies


hsriat
Expert 100+
P: 1,654
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, ... ... ...)")
Jun 16 '09 #2

Markus
Expert 5K+
P: 6,050
You could just use one of the built in random functions or hash functions.
Jun 16 '09 #3

P: 40
thank hsriat..its working.... :)
Jun 17 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.