irwan

Sending an Email Confirmation Link with PHP

Nov 20th, 2011
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.17 KB | None | 0 0
  1. Making the form:
  2.  
  3. code:
  4. <form name="emailInput" action="emailInput.php" method="post">
  5.   Username: <input type="text" name="username" size="40" maxlength="255" /><br/>
  6.   Email: <input type="text" name="email" size="40" maxlength="255" /><br/>
  7.   <input type="submit" value="Create Account" />
  8. </form>
  9.  
  10. Validating Input and Sending Confirmation Link:
  11. In this section, we want to make sure that the person entered something into the username and email textboxes. If they did, we will create an 8 digit key and store that in the database along with their username and email. After storing the information, we will send them an email with the confirmation link in it.
  12.  
  13. The first thing we will do is create a function to handle the random string for the confirmation link. Here is what the random function will look like:
  14.  
  15. <?php
  16. function getRandomString()
  17. {
  18.   $length = 8;
  19.  
  20.   //string of all possible characters to go into the new password
  21.   $passwordRandomString = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789";
  22.  
  23.   //initialize the new password string
  24.   $newPW = "";
  25.  
  26.   //seed the random function
  27.   srand();
  28.  
  29.   //go through to generate a random password.
  30.   for($x=0; $x < $length; $x++)
  31.   {
  32.     $newPW .= substr($passwordRandomString,rand(0,62),1);
  33.   }
  34.  
  35.   return $newPW;
  36. }
  37. ?>
  38.  
  39. Here is what the rest of the "emailInput.php" page will look like:
  40.  
  41. <?php
  42.   //database connection goes here
  43.   //...
  44.  
  45.   //get input data
  46.   $username = $_POST['username'];
  47.   $email = $_POST['email'];
  48.   $errorMessage = "";
  49.  
  50.   //check to make sure they're not empty.
  51.   if(empty($username)) $errorMessage .= "You didn't enter a username.<br/>\n";
  52.   if(empty($email)) $errorMessage .= "You didn't enter an email address.<br/>\n";
  53.  
  54.   //if there was no error, do the rest of the code.
  55.   if(empty($errorMessage))
  56.   {
  57.     //get a random 8 character string.
  58.     $confirmationCode = getRandomString();
  59.    
  60.     //add the person and the confirmation code to the database.
  61.     $query = "INSERT INTO `users` (`username`, `email`, `confirmationCode`, `confirmed`)
  62.                    VALUES ('$username','$email','$confirmationCode','0')";
  63.  
  64.     $result = mysql_query($query) OR die(mysql_error());
  65.  
  66.     //now we want to send them an email telling them to confirm their account.
  67.     $to = "";
  68.     $subject = "";
  69.     $message="";
  70.     $headers="";
  71.    
  72.     /* recipients */
  73.     $to  = $email;
  74.    
  75.     /* subject */
  76.     $subject = "Site Registration Confirmation";
  77.    
  78.     /* message */
  79.     $message = '<html>
  80. <head>
  81. <title>Site Registration Confirmation</title>
  82. </head>
  83.  
  84. <body style="font-family:verdana, arial; font-size: .8em;">
  85. You\'re receiving this email because you filled out a registration form on this website.
  86. <br/><br/>
  87. If you did not try to create an account, you can simply delete this email. No further action is required.
  88. <br/><br/>
  89. To complete confirmation and add your registration, please click on the link below:<br>
  90. <a title="Confirm Comment"
  91. href="http://www.website.com/confirm.php?c='.$confirmationCode.'">http://www.website.com/confirm.php?c='.$confirmationCode.'</a>
  92. <br/><br/>
  93. Thank you and we hope you enjoy using Website.com!<br/><br/>
  94.  
  95. </body>
  96. </html>';
  97.  
  98.     /* To send HTML mail, you can set the Content-type header. */
  99.     $headers  = "MIME-Version: 1.0\r\n";
  100.     $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
  101.    
  102.     /* additional headers */
  103.     $headers .= "From: Website <[email protected]>\r\n";
  104.    
  105.     /* and now mail it */
  106.     mail($to, $subject, $message, $headers);
  107.  
  108.     //let them know what's going on.
  109.     echo 'Click the link in the email to confirm your account.';
  110.   }
  111.   else
  112.   {
  113.     echo $errorMessage;
  114.   }
  115. ?>
  116. In the query to add the user to the database, I also included a variable called "confirmed" and set it to "0." I did this because we need some way of knowing if they have clicked on the confirmation link or not. When they click on the confirmation link, we will set the value to "1."
  117.  
  118.  
  119.  
  120. Creating the Confirm page:
  121. Now that the email has been sent and the users information is stored in the database, we need to find some way to let them confirm it. To do this, we will create a new PHP page called "confirm.php." Here is what confirm.php will look like:
  122.  
  123. <?php
  124.   $confirmId = $_GET['c'];
  125.  
  126.   $errorMessage = "";
  127.   $validCount = 0;
  128.  
  129.   $query = "SELECT confirmed FROM `users` WHERE confirmationCode='$confirmId'";
  130.   $result = mysql_query($query);
  131.   $validCount = mysql_num_rows($result);
  132.  
  133.   if($result['confirmed'] == 1) $errorMessage .= "You have already confirmed this comment.<br/>";
  134.   if($validCount == 0) $errorMessage .= "You are trying to confirm an invalid comment.<br/>";
  135.  
  136.   if(empty($errorMessage))
  137.   {    
  138.     $query = "UPDATE `users` SET confirmed = 1 WHERE confirmationCode='$confirmId'";
  139.     mysql_query($query) OR die(mysql_error());
  140.  
  141.     echo 'Your account has been confirmed!';
  142.   }
  143.   else
  144.   {
  145.     echo $errorMessage;
  146.   }
  147. ?>
  148.  
  149. This code checks to make sure that their account isn't already confirmed, and that the confirmation code they entered is valid. If there is no error with the input, the "confirmed" value in the table is changed to 1 and they are now valid.
  150.  
  151.  
Advertisement
Add Comment
Please, Sign In to add comment