After a new user fills out the registration form on my site, I would like a random (if possible) password sent to them at the address they entered.

Currently they enter their own password which allows for bogus user emails.

Anyone know the correct code for this?

    Im not sure what you mean that it allows for bogus user emails....

    But to generate a random password you can take several available scripts taht generate random alphanumeric characters and get a random password. You would also need to store this password in the database so that the user can login for the first time and change the password.

    Search google for, generate random password PHP

    One of the links lead me to this code....you will have to figure what you want the password to be:

    ?php
    
    
    
    /**
    
     * The letter l (lowercase L) and the number 1
    
     * have been removed, as they can be mistaken
    
     * for each other.
    
     */
    
    
    
    function createRandomPassword() {
    
    
    
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    
    srand((double)microtime()*1000000);
    
    $i = 0;
    
    $pass = '' ;
    
    
    
    while ($i <= 7) {
    
        $num = rand() % 33;
    
        $tmp = substr($chars, $num, 1);
    
        $pass = $pass . $tmp;
    
        $i++;
    
    }
    
    
    
    return $pass;
    
    
    
    }
    
    
    
    // Usage
    
    $password = createRandomPassword();
    
    echo "Your random password is: $password";
    
    
    
    ?> 
    

      Thanks, that's helpful.

      The bogus email is a fake email. Users can enter anything they want and not their real email address.

      In the function below, do I store a value in this line - $pass = ' ' ?

      function createRandomPassword() {

      $chars = "abcdefghijkmnopqrstuvwxyz023456789";
      
      srand((double)microtime()*1000000);
      
      $i = 0;
      
      $pass = '' ;

        Nope! Thats just a declaration.

        Because the following code in the while loop is telling PHP to store whatever the value of $pass is back into $pass plus additional characters

        $pass = $pass . $tmp;

        . If the developer didn't not put $pass= ''; then the script would give an error telling you that it cant find $pass

          note that this is just an example....you can modify it to suite your needs.

            One of the links lead me to this code....you will have to figure what you want the password to be:

            Unfortunately, it is not very well written. For example, it uses substr() when string indexing will do, a while loop when a for loop more clearly expresses the loop, a hard coded number of iterations, and srand(), which is no longer necessary. I would suggest:

            function createRandomPassword($length = 8) {
                $chars = "abcdefghijkmnopqrstuvwxyz023456789";
                $max = strlen($chars) - 1;
            
            $pass = '' ;
            for ($i = 0; $i < $length; ++$i) {
                $pass .= $chars[mt_rand(0, $max)];
            }
            return $pass;
            }

            In the function below, do I store a value in this line - $pass = ' ' ?

            You would use the return value of the function, e.g.,

            $password = createRandomPassword();

              I agree, its a very basic example.

              Follow this link and you can explore other algorithms and ideas to generate random passwords.

              Search Results

                So now I have added the createRandomPassword function, I need it to insert the password into the db. Does this require a "hidden" form field?
                Also, what is the "action" of the form as I need it to insert the Registration fields AND send the user his random password (SendMail).
                Sorry, I do not have a coders brain!

                  So now I have added the createRandomPassword function, I need it to insert the password into the db. Does this require a "hidden" form field?

                  No, you would generate the password upon registration, not during registration.

                    Write a Reply...