\r\nCREATE TABLE `users` (\r\n `user_id` int(7) unsigned NOT NULL auto_increment,\r\n `display_name` varchar(20) NOT NULL,\r\n `password` varchar(255) NOT NULL,\r\n `first_name` varchar(25) NOT NULL,\r\n `last_name` varchar(25) NOT NULL,\r\n `email_address` varchar(255) NOT NULL,\r\n PRIMARY KEY (`user_id`)\r\n) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0;\r\n<\/pre>\nNow 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\u2019t 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.)<\/p>\n
\r\n<form method="post" action="register.php" name="createuser">\r\n\tUserName:<input type="text" name="display_name" id="display_name"\/><br \/>\r\n\tFirst Name:<input type="text" name="fname" id="name"\/><br \/>\r\n\tLast Name:<input type="text" name="lname" id="surname"\/><br \/>\r\n\tE-mail:<input type="text" name="email" id="email"\/><br \/>\r\n\tPassword:<input type="password" name="pass" id="pass"\/><br \/>\r\n\tConfirm PW:<input type="password" name="pass2" id="pass2"\/><br \/>\r\n\t<input type="submit" value="Sign Me Up!"\/>\r\n<\/form>\r\n<\/pre>\nNext, we will need to create the register.php file that will be called when the form is submitted. <\/p>\n
\r\n<?php\r\nif ((!isset($_POST['display_name']))||(!isset($_POST['fname']))||(!isset($_POST['lname']))||(!isset($_POST['email']))||(!isset($_POST['pass']))){\r\n\t\theader('Location: http:\/\/www.teamtutorials.com');\r\n\t}\r\n[\/sourcecode]\r\n\r\nFirst, 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.\r\n\r\n[sourcecode language='php']\r\nelse\r\n\t{\r\n\t\t$servername='localhost';\r\n\t\t$dbusername='dbusername';\r\n\t\t$dbpassword='dbpassword';\r\n\t\t$dbname='full db name';\r\n\t\tglobal $link;\r\n\t\t$link=mysql_connect ($servername,$dbuser,$dbpassword);\r\n\t\tif(!$link){die("Could not connect to MySQL");}\r\n\t\tmysql_select_db($dbname,$link) or die ("could not open db".mysql_error());\r\n[\/sourcecode]\r\n\r\nIf 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.\r\n\r\n[sourcecode language='php']\r\n\t\t$display_name = $_POST['display_name'];\r\n\t\t$password = $_POST['pass'];\r\n\t\t$first_name = $_POST['fname'];\r\n\t\t$last_name = $_POST['lname'];\r\n\t\t$email_address = $_POST['email'];\r\n\t\t$sha_password = sha1($password);\r\n\t\t$hash_string = hash('md5',$display_name);\t\r\n[\/sourcecode]\r\n\r\nThis just stores all the values into variables that are easier to work with so we don\u2019t 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\u2019t easily readable. We use the display name because it will be unique and specific to the user. \r\n\r\n[sourcecode language='php']\r\n$query = "insert into `users` values(Null,'$display_name','$sha_password','$first_name','$last_name','$email_address',0,1,CURRENT_TIMESTAMP,Null,'$company_name',8);";\r\n\t\tmysql_query($query) or die ("Error in query: $query " . mysql_error());\r\n\t\t$user_id = mysql_insert_id();\r\n[\/sourcecode]\r\n\r\nNext, 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.\r\n\r\n[sourcecode language='php']\r\n\t\t$headers = "From: activations@teamtutorials.com \\r\\n";\r\n\t\t$validate_link = "http:\/\/teamtutorials.com\/validate.php?id=$user_id&string=$hash_string";\r\n\t\t$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";\r\n[\/sourcecode]\r\n\r\nThis 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\u2019t 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.\r\n\r\n[sourcecode language='php']\r\n\t\tif (mail($email_address,"TeamTutorials Sign-Up",$email_body,$headers)){\r\n\t\t\techo "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 \t\te-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.";\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\techo "There was an error sending an e-mail to your e-mail address. Please contact us to let us know of the issue.";\r\n\t\t}\r\n\t}\r\n?>\r\n<\/pre>\nFinally 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.<\/p>\n
\r\n<?php\r\nif (isset($_GET['id']))&&(isset($_GET['id'])){\r\n\t$id = $_GET['id']; \r\n\t$hashstring = $_GET['string']; \r\n\t$storedhashvalue = "";\r\n[\/sourcecode]\r\n\r\nFirst 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\u2019t set.\r\n\r\n[sourcecode language='php']\r\n$query = "select user_id,display_name from `users` where user_id=$id;";\r\n\t$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());\r\n\t$row = mysql_fetch_assoc($result);\r\n\t$storedhashvalue = hash('md5',$row['display_name']);\r\n\tmysql_free_result($result);\r\n[\/sourcecode]\r\n\r\nThese 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.\r\n\r\n[sourcecode language='php']\r\n\tif ($storedhashvalue == $hashstring){\r\n\t\t$query = "update `users` set active_flag=1 where user_id=$id;";\r\n\t\t$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());\r\n\t\techo "Your account has been activated. Please <a href='http:\/\/teamtutorials.com\/login.php>Click Here To Login<\/a>";\r\n\t}\r\n\telse\r\n\t{\r\n\t\techo "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.";\r\n\t}\r\n<\/pre>\nIf the values match we update the database to see the user as active and if it doesn\u2019t work we tell them that it failed. <\/p>\n
\r\n}\r\nelse\r\n{\r\nheader('Location: http:\/\/www.teamtutorials.com');\r\n}\r\n?>\r\n<\/pre>\nFinally 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.<\/p>\n","protected":false},"excerpt":{"rendered":"
In on of our last tutorials we covered how to verify that a user\u2019s email address is formatted correctly<\/a> 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.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[24,17],"tags":[97,47,58,42],"yoast_head":"\nSending E-Mail to validate User Sign-up<\/title>\n \n \n \n \n\t \n\t \n\t \n