{"id":1996,"date":"2009-07-22T21:06:51","date_gmt":"2009-07-23T01:06:51","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=1996"},"modified":"2009-07-22T21:08:42","modified_gmt":"2009-07-23T01:08:42","slug":"sending-e-mail-to-validate-user-sign-up","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/web-development-tutorials\/sending-e-mail-to-validate-user-sign-up","title":{"rendered":"Sending E-Mail to validate User Sign-up"},"content":{"rendered":"

In one 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. This will require them to click on the link in the e-mail before they can actually login to the site. Let\u2019s get started. First, let\u2019s 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.<\/p>\n

\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>\n

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\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>\n

Next, 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&#91;'display_name'&#93;))||(!isset($_POST&#91;'fname'&#93;))||(!isset($_POST&#91;'lname'&#93;))||(!isset($_POST&#91;'email'&#93;))||(!isset($_POST&#91;'pass'&#93;))){\r\n\t\theader('Location: http:\/\/www.teamtutorials.com');\r\n\t}\r\n&#91;\/sourcecode&#93;\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&#91;sourcecode language='php'&#93;\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&#91;\/sourcecode&#93;\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&#91;sourcecode language='php'&#93;\r\n\t\t$display_name = $_POST&#91;'display_name'&#93;;\r\n\t\t$password = $_POST&#91;'pass'&#93;;\r\n\t\t$first_name = $_POST&#91;'fname'&#93;;\r\n\t\t$last_name = $_POST&#91;'lname'&#93;;\r\n\t\t$email_address = $_POST&#91;'email'&#93;;\r\n\t\t$sha_password = sha1($password);\r\n\t\t$hash_string = hash('md5',$display_name);\t\r\n&#91;\/sourcecode&#93;\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&#91;sourcecode language='php'&#93;\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&#91;\/sourcecode&#93;\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&#91;sourcecode language='php'&#93;\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&#91;\/sourcecode&#93;\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&#91;sourcecode language='php'&#93;\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>\n

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.<\/p>\n

\r\n<?php\r\nif (isset($_GET&#91;'id'&#93;))&&(isset($_GET&#91;'id'&#93;)){\r\n\t$id = $_GET&#91;'id'&#93;; \r\n\t$hashstring = $_GET&#91;'string'&#93;; \r\n\t$storedhashvalue = "";\r\n&#91;\/sourcecode&#93;\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&#91;sourcecode language='php'&#93;\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&#91;'display_name'&#93;);\r\n\tmysql_free_result($result);\r\n&#91;\/sourcecode&#93;\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&#91;sourcecode language='php'&#93;\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>\n

If 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>\n

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.<\/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<meta name=\"description\" content=\"In on of our last tutorials we covered how to verify that a user\u2019s 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.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/teamtutorials.com\/web-development-tutorials\/sending-e-mail-to-validate-user-sign-up\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mike Maguire\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/teamtutorials.com\/#website\",\"url\":\"https:\/\/teamtutorials.com\/\",\"name\":\"Team Tutorials\",\"description\":\"Learn something new today\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/teamtutorials.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/sending-e-mail-to-validate-user-sign-up#webpage\",\"url\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/sending-e-mail-to-validate-user-sign-up\",\"name\":\"Sending E-Mail to validate User Sign-up\",\"isPartOf\":{\"@id\":\"https:\/\/teamtutorials.com\/#website\"},\"datePublished\":\"2009-07-23T01:06:51+00:00\",\"dateModified\":\"2009-07-23T01:08:42+00:00\",\"author\":{\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b\"},\"description\":\"In on of our last tutorials we covered how to verify that a user\\u2019s 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.\",\"breadcrumb\":{\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/sending-e-mail-to-validate-user-sign-up#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/teamtutorials.com\/web-development-tutorials\/sending-e-mail-to-validate-user-sign-up\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/sending-e-mail-to-validate-user-sign-up#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/teamtutorials.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sending E-Mail to validate User Sign-up\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b\",\"name\":\"Mike Maguire\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r\",\"caption\":\"Mike Maguire\"},\"sameAs\":[\"http:\/\/mikemaguire.me\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Sending E-Mail to validate User Sign-up","description":"In on of our last tutorials we covered how to verify that a user\u2019s 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.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/teamtutorials.com\/web-development-tutorials\/sending-e-mail-to-validate-user-sign-up","twitter_misc":{"Written by":"Mike Maguire","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/teamtutorials.com\/#website","url":"https:\/\/teamtutorials.com\/","name":"Team Tutorials","description":"Learn something new today","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/teamtutorials.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/sending-e-mail-to-validate-user-sign-up#webpage","url":"https:\/\/teamtutorials.com\/web-development-tutorials\/sending-e-mail-to-validate-user-sign-up","name":"Sending E-Mail to validate User Sign-up","isPartOf":{"@id":"https:\/\/teamtutorials.com\/#website"},"datePublished":"2009-07-23T01:06:51+00:00","dateModified":"2009-07-23T01:08:42+00:00","author":{"@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b"},"description":"In on of our last tutorials we covered how to verify that a user\u2019s 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.","breadcrumb":{"@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/sending-e-mail-to-validate-user-sign-up#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/teamtutorials.com\/web-development-tutorials\/sending-e-mail-to-validate-user-sign-up"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/sending-e-mail-to-validate-user-sign-up#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/teamtutorials.com\/"},{"@type":"ListItem","position":2,"name":"Sending E-Mail to validate User Sign-up"}]},{"@type":"Person","@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b","name":"Mike Maguire","image":{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r","caption":"Mike Maguire"},"sameAs":["http:\/\/mikemaguire.me"]}]}},"_links":{"self":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/1996"}],"collection":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/comments?post=1996"}],"version-history":[{"count":0,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/1996\/revisions"}],"wp:attachment":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media?parent=1996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/categories?post=1996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/tags?post=1996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}