We’d like to remind Forumites to please avoid political debate on the Forum.

This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.

📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!

PHP script

Hi guys,

Does anyone know of a PHP script that can do the following?

* Allow users to enter a username of their choice via a web form
* Randomly generate a password for the user and email it to them on form submission
* Store this information either in a MySQL database, or email the results to a storage address


Any help would be much appreciated, as I've spent the last hour and a half trawling around for one to no avail! :confused:
No longer visiting these forums.

Comments

  • Sounds like it would only take a short while to write anyway, it's probably too small a task to warrant a script you can just download.
  • Wombat
    Wombat Posts: 960 Forumite
    Part of the Furniture 500 Posts Combo Breaker
  • Astaroth
    Astaroth Posts: 5,444 Forumite
    Yup, very basic requirements. Any server side scripting language can do this.
    All posts made are simply my own opinions and are neither professional advice nor the opinions of my employers
    No Advertising or Links in Signatures by Site Rules - MSE Forum Team 2
  • LewisC_2
    LewisC_2 Posts: 401 Forumite
    Yep - had guessed it would be very basic, but then again so is my knowledge of PHP :)

    Anybody able to give me a hand with getting something coded at all?
    No longer visiting these forums.
  • moneyuser
    moneyuser Posts: 1,085 Forumite
    Part of the Furniture 1,000 Posts Combo Breaker
    Put this

    Generate Password

    together with this

    Send email

    and create a form and with a little bit of tweaking you will get what you want
  • LewisC_2
    LewisC_2 Posts: 401 Forumite
    moneyuser wrote: »
    Put this

    Generate Password

    together with this

    Send email

    and create a form and with a little bit of tweaking you will get what you want

    Both links go to the same place!

    As I mentioned before, my PHP knowledge is very basic, so I'm still none the wiser. Anyone?
    No longer visiting these forums.
  • LewisC wrote: »
    Hi guys,

    Does anyone know of a PHP script that can do the following?

    * Allow users to enter a username of their choice via a web form
    * Randomly generate a password for the user and email it to them on form submission
    * Store this information either in a MySQL database, or email the results to a storage address


    Any help would be much appreciated, as I've spent the last hour and a half trawling around for one to no avail! :confused:

    Can you let us know why you need it and in what format you will be using it - i.e. is it to be on an existing forum or webpage?
    To be able under all circumstances to practice five things constitutes perfect virtue; these five things are gravity, generosity of soul, sincerity, earnestness and kindness.
    Confucius
  • moneyuser
    moneyuser Posts: 1,085 Forumite
    Part of the Furniture 1,000 Posts Combo Breaker
    Ok, I changed the link to the correct one.

    I've put this together for you.

    Please note that the html and php may not meet any required standards or what is considered "correct html" and certainly does not meet any security standards and therefore cannot be considered as "hacker proof" (if there is such a thing). However it should provide you with a good starting point.

    We have a html form where the user enters their username and email address. The user then presses the submit button which calls the php script. The php script will generate the password and email the password to the user using the email address they entered. An email will also be sent to your "storage" address. The storage address is defined in the php script (just after the end of the generatePassword function). This should be the only thing you need to change to get it to work. Of couse you might want to change what details are sent to the addresses, what info is displayed on screen, how the data is formatted etc. The email validation needs looking at.

    IMPORTANT NOTE: After previewing the code it has messed up the opening braces, so where ever you see !! needs to be changed to an opening brace shift+[ (next to P)

    Here is the html form. Copy the text and save it to a file named autopass.html.
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
    <HTML><HEAD><TITLE>Generate Password</TITLE></HEAD>
    <body>
    
    <form method="POST" action="autopass.php">
      <p><b>Username</b> <input type="text" name="txtUsername" size="20"></p>
      <p><b>Email&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>
      <input type="text" name="txtEmail" size="100"></p>
      <input type="submit" value="Submit" name="btnSubmit">
      <input type="reset" value="Reset" name="btnReset">
    </form>
    </body>
    </html>
    


    Here is the php script that is called by the form. Copy the text and save it as autopass.php.
    <?php
    
    // Heinz Tschabitscher
    // How to Send Email from a PHP Script
    //http://email.about.com/cs/phpemailtips/qt/et031202.htm
    
    function SendEmail($to, $subject, $body)
    !!
      //$to = "recipient&#64;example.com";
      //$subject = "Hi!";
      //$body = "Hi,\n\nHow are you?";
      if (mail($to, $subject, $body)) 
      !!
        //echo("<p>Message successfully sent!</p>");
        return true;
      } 
      else 
      !!
       // echo("<p>Message delivery failed...</p>");
        return false;
      }  
    } 
    
    // Jon Haworth
    // PHP: Generate random password
    // http://www.laughing-buddha.net/jon/php/password/
    function generatePassword ($length = 8)
    !!
    
      // start with a blank password
      $password = "";
    
      // define possible characters
      $possible = "0123456789bcdfghjkmnpqrstvwxyz"; 
        
      // 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;
    }
    
    // Email address for admin
    $adminEmail = "youremail&#64;youremail.com";
    
    $username = $_POST[txtUsername];
    $email = $_POST[txtEmail];
    
    print "<BR>Username is " . $username; 
    $password = generatePassword();
    print "<BR>Password is " . $password; 
    print "<BR>Email is " . $email; 
    
    // Create email body
    $emailBody = "Username is " . $username . 
                 "\nPassword is " . $password . 
                 "\nEmail is " . $email;
    
    // Send email to Admin
    if( SendEmail( $adminEmail, "New User", $emailBody ) )
    !!
      echo("<p>Message successfully sent to admin!</p>");
    }
    else
    !!
      echo("<p>Admin delivery failed</p>");
    }
    
    // Send email to user
    $emailBody = " Your password is " . $password;
    if( SendEmail( $email, "Your password", $emailBody ) )
    !!
      echo("<p>Message successfully sent to User!</p>");
    }
    else
    !!
      echo("<p>User delivery failed</p>");
    }
    ?>
    
  • LewisC_2
    LewisC_2 Posts: 401 Forumite
    moneyuser wrote: »
    Ok, I changed the link to the correct one.

    I've put this together for you.

    Please note that the html and php may not meet any required standards or what is considered "correct html" and certainly does not meet any security standards and therefore cannot be considered as "hacker proof" (if there is such a thing). However it should provide you with a good starting point.

    We have a html form where the user enters their username and email address. The user then presses the submit button which calls the php script. The php script will generate the password and email the password to the user using the email address they entered. An email will also be sent to your "storage" address. The storage address is defined in the php script (just after the end of the generatePassword function). This should be the only thing you need to change to get it to work. Of couse you might want to change what details are sent to the addresses, what info is displayed on screen, how the data is formatted etc. The email validation needs looking at.

    IMPORTANT NOTE: After previewing the code it has messed up the opening braces, so where ever you see !! needs to be changed to an opening brace shift+[ (next to P)

    Thanks very much. Can tidy up the HTML no problem. I hand-code the entire site in XHTML 1.0 Transitional anyway, so that won't be a problem.

    Security isn't an issue either, so will give that a whirl - thanks :)
    No longer visiting these forums.
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 352.9K Banking & Borrowing
  • 253.9K Reduce Debt & Boost Income
  • 454.7K Spending & Discounts
  • 245.9K Work, Benefits & Business
  • 602K Mortgages, Homes & Bills
  • 177.8K Life & Family
  • 259.8K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16K Discuss & Feedback
  • 37.7K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.