Random password generation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • divyac
    New Member
    • Aug 2008
    • 40

    Random password generation

    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..pleas e help me..
    Code:
        function generatePassword ( $length = 8 ) {
        // start with a blank password
        $password = “”;
    
        // define possible characters
        $possible = “0123456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ!@#$%^&*”;
    
        // set up a counter
        $i = 0;
    
        // add random characters to $password until $length is reached
        while ($i < $length) {
        // pick a random character from the possible ones
        $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
    
        // we don’t want this character if it’s already in the password
        if (!strstr($password, $char)) {
        $password .= $char;
        $i++;
        }
        }
        // done!
        return $password;
        }
    where should i call this function generatePasswor d????
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    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
    Code:
    $new_password = generatePassword (10); //whatever length
    $sql = mysql_query("INSERT INTO ..... .... (`password`, ... ... ...) VALUES ($new_password, ... ... ...)")

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      You could just use one of the built in random functions or hash functions.

      Comment

      • divyac
        New Member
        • Aug 2008
        • 40

        #4
        thank hsriat..its working.... :)

        Comment

        Working...