Check out our newest Tutorial project. Tutorial Grad the site that submits your tutorials for you.

Subscribe to our rss feed

Sending E-Mail to validate User Sign-up

Posted in PHP Tutorials, Web Development Tutorials by Mike Maguire on the July 22nd, 2009

In one of our last tutorials we covered how to verify that a user’s email address is formatted correctly as well as verify that it goes to a valid domain name. That is great, but still not a great way to make sure a user actually owns or uses that e-mail address. This tutorial will walk you through one method of sending the user an e-mail upon signing up that gives them a link to activate their account. This will require them to click on the link in the e-mail before they can actually login to the site. Let’s get started. First, let’s create a table to hold our user information. I made a database called teamtutorials on my local server and ran this to create the table.

CREATE TABLE `users` (
  `user_id` int(7) unsigned NOT NULL auto_increment,
  `display_name` varchar(20) NOT NULL,
  `password` varchar(255) NOT NULL,
  `first_name` varchar(25) NOT NULL,
  `last_name` varchar(25) NOT NULL,
  `email_address` varchar(255) NOT NULL,
  PRIMARY KEY  (`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0;

Now that the table is set up and configured the way we need it to be we can start creating the php files. We will start with the simple form file. This is adduser.php (it doesn’t have to be a php file but I do it so that If I add something later that is php it is already a php file.)

<form method="post" action="register.php" name="createuser">
	UserName:<input type="text" name="display_name" id="display_name"/><br />
	First Name:<input type="text" name="fname" id="name"/><br />
	Last Name:<input type="text" name="lname" id="surname"/><br />
	E-mail:<input type="text" name="email" id="email"/><br />
	Password:<input type="password" name="pass" id="pass"/><br />
	Confirm PW:<input type="password" name="pass2" id="pass2"/><br />
	<input type="submit" value="Sign Me Up!"/>
</form>

Next, we will need to create the register.php file that will be called when the form is submitted.

<?php
if ((!isset($_POST['display_name']))||(!isset($_POST['fname']))||(!isset($_POST['lname']))||(!isset($_POST['email']))||(!isset($_POST['pass']))){
		header('Location: http://www.teamtutorials.com');
	}

First, we start the php tag and then check for the POST variables to be set. If any of them are not set it will send them back to the creation form.

else
	{
		$servername='localhost';
		$dbusername='dbusername';
		$dbpassword='dbpassword';
		$dbname='full db name';
		global $link;
		$link=mysql_connect ($servername,$dbuser,$dbpassword);
		if(!$link){die("Could not connect to MySQL");}
		mysql_select_db($dbname,$link) or die ("could not open db".mysql_error());

If all of the POST variables are set we continue by setting the server variables and creating the database connection. Ensure that you change the variables to your server access information.

		$display_name = $_POST['display_name'];
		$password = $_POST['pass'];
		$first_name = $_POST['fname'];
		$last_name = $_POST['lname'];
		$email_address = $_POST['email'];
		$sha_password = sha1($password);
		$hash_string = hash('md5',$display_name);

This just stores all the values into variables that are easier to work with so we don’t have to call the POST global every time. We sha1 the password because it is not reversible so it makes it the most secure. The hash_string is a md5 hash value of the display_name so that it isn’t easily readable. We use the display name because it will be unique and specific to the user.

$query = "insert into `users` values(Null,'$display_name','$sha_password','$first_name','$last_name','$email_address',0,1,CURRENT_TIMESTAMP,Null,'$company_name',8);";
		mysql_query($query) or die ("Error in query: $query " . mysql_error());
		$user_id = mysql_insert_id();

Next, we insert a row into our table that we made using the variables that were passed from the form. We also get the ID of the inserted row so we know what the user id will be.

		$headers = "From: activations@teamtutorials.com \r\n";
		$validate_link = "http://teamtutorials.com/validate.php?id=$user_id&string=$hash_string";
		$email_body = "Thank-you for signing up on TeamTutorials. Click on the link below to complete your registration. If you have any issues completing the verfication please let us know. \n\n $validate_link \n\n TeamTutorials Staff";

This builds all the information needed to generate an e-mail using php. The headers sets the from field so that the email will be from a user (if you don’t set this it will be nobody (the user which apache runs under in linux). The validate link is a link to the file that will validate the user when they click on it. The \n command is the php new line command.

		if (mail($email_address,"TeamTutorials Sign-Up",$email_body,$headers)){
			echo "Email has been sent to ".$email_address.". Please check your e-mail for steps to activate your account. Check your spam folder as sometimes these 		e-mail get marked as spam. If you still do not see your e-mail, please <a href='http://teamtutorials.com/sendemail.php?function=validation&id=$user_id'> Click Here</a> to resend.";
		}
		else
		{
			echo "There was an error sending an e-mail to your e-mail address. Please contact us to let us know of the issue.";
		}
	}
?>

Finally we attempt to send the e-mail and echo success or failure. That concludes this file. Finally we need to make the file that will validate the user when they click on the link in the e-mail that we just sent them. This file is validate.php.

<?php
if (isset($_GET['id']))&&(isset($_GET['id'])){
	$id = $_GET['id'];
	$hashstring = $_GET['string'];
	$storedhashvalue = "";

First we make sure the values are in the url that we are expecting. At the end of the file we will re-direct them to the home page if these values aren’t set.

$query = "select user_id,display_name from `users` where user_id=$id;";
	$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
	$row = mysql_fetch_assoc($result);
	$storedhashvalue = hash('md5',$row['display_name']);
	mysql_free_result($result);

These lines run a query against the table to get the information for the user based on the user id in the url. It then re-hashes the value so that we can compare the on in the url to match.

	if ($storedhashvalue == $hashstring){
		$query = "update `users` set active_flag=1 where user_id=$id;";
		$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
		echo "Your account has been activated. Please <a href='http://teamtutorials.com/login.php>Click Here To Login</a>";
	}
	else
	{
		echo "Your account could not be verified. Please verify that the link has not been modified from the e-mail. If it still does not work, please contact us.";
	}

If the values match we update the database to see the user as active and if it doesn’t work we tell them that it failed.

}
else
{
header('Location: http://www.teamtutorials.com');
}
?>

Finally we re-direct the user if the variables are not in the url. Now if you go to adduser.php and fill out the form and hit submit. It will send whatever e-mail you put in the form an e-mail with a link in it. Click on the link to activate the user. That concludes this tutorial. If you have any questions, please leave it in the comments. Thanks for viewing.

Popularity: 5% [?]

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Reddit

Related Posts

Tutorial Grad - Recent Tutorials

11 Responses to 'Sending E-Mail to validate User Sign-up'

Subscribe to comments with RSS or TrackBack to 'Sending E-Mail to validate User Sign-up'.

  1. Charlie said,

    on August 4th, 2009 at 6:40 am

    Excellent tip – thanks for this.

  2. Buy PSP Go said,

    on August 10th, 2009 at 6:42 am

    Thanks for this code, it will really help when looking to edit some pre-existing scripts I have.

  3. football said,

    on September 4th, 2009 at 5:10 am

    Hi,it is really a nice and important article with good info related to Sending E-Mail to validate User Sign-up……..tips.


  4. on September 5th, 2009 at 1:20 pm

    Thanks for this, I was looking for some information on how to do this. It can be very hard to get a validation email sent.


  5. on September 12th, 2009 at 10:13 am

    [...] July 22, 2009 — Sending E-Mail to validate User Sign-up (5) [...]


  6. on September 19th, 2009 at 5:32 pm

    This is a very nice way get this done, thanks for the tutorial.

  7. PSP Go said,

    on October 6th, 2009 at 12:25 pm

    Very handy and useful information to have. Thanks for including this.

  8. Wolf20 said,

    on October 10th, 2009 at 8:18 pm

    Thank you Pamela for the information and congratulations Dr. ,


  9. on January 5th, 2010 at 2:52 pm

    Is there any way to validate the form so that it dosen’t give an error message until the click “Submit”?

  10. Cetvn said,

    on January 24th, 2010 at 1:44 am

    this is very helpful code thanks for sharing.

  11. Megan Fox said,

    on January 27th, 2010 at 6:31 pm

    I am trying to learn the basis of email validation. Great tutorial Mike, hope to see more in the future.

Leave a Comment