Secure Login and Registration

This tutorial we will teach you on how to build a Secure Login and Registration system whereby users of your websites will be able to create their accounts, log in with their valid information so as to access their various accounts. Users information during sign up process are validated and stored in the database. The system validates new users for duplicate email addresses and performs valid authentication during registered users log in. And this is compose of PHP and MySql, this programs are written in a way that any one can understand and customize.

Sample Code

login.php - This is for the login activity of every users, and it has a script that gets a data in the database so the can user can login to their account.
  1. <?php
  2. include "database_connection.php";
  3.  
  4. if(isset($_POST["submitted"]) && $_POST["submitted"] == "yes")
  5. {
  6. $user_email = trim(strip_tags($_POST['email']));
  7. $user_password = trim(strip_tags($_POST['passwd']));
  8. $encrypted_md5_password = md5($user_password);
  9. $validate_user_information = mysql_query("select * from `signup_and_login_table` where `email` = '".mysql_real_escape_string($user_email)."' and `password` = '".mysql_real_escape_string($encrypted_md5_password)."'");
  10.  
  11. if($user_email == "" || $user_password == "")
  12. {
  13. $error = '<br><div class="info">Sorry, all fields are required to log into your account. Thanks.</div><br>';
  14. }
  15. elseif(mysql_num_rows($validate_user_information) == 1)
  16. {
  17. $get_user_information = mysql_fetch_array($validate_user_information);
  18. $_SESSION["VALID_USER_ID"] = $user_email;
  19. $_SESSION["USER_FULLNAME"] = strip_tags($get_user_information["firstname"].'&nbsp;'.$get_user_information["lastname"]);
  20. header("location: index.php");
  21. }
  22. else
  23. {
  24. $error = '<br><div class="info">Sorry, you have provided incorrect information. Please enter correct user information to proceed. Thanks.</div><br>';
  25. }
  26. }
  27. ?>
  28. <!DOCTYPE html>
  29. <html>
  30. <head>
  31. <title>Secure Login and Registration</title>
  32. <link href="css/style.css" rel="stylesheet" type="text/css">
  33. </head>
  34. <body>
  35. <center>
  36. <br /><br /><br /><br /><div style="font-family:Verdana, Geneva, sans-serif; font-size:24px;"><h3>Secure Login and Registration</h3></div>
  37. <center>
  38. <div class="main_wrapper">
  39. <br clear="all">
  40. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  41. <h2 align="center" style="margin-top:0px;">User Login</h2><hr/><br/>
  42. <div style="width:115px;float:left;" align="left"><h3>Email Address:</h3></div>
  43. <div style="width:300px;float:left;" align="left"><input type="text" name="email" id="email" value="" class="textAreaBoxInputs" placeholder="Email Address"></div><br clear="all"><br clear="all">
  44. <div style="width:115px;float:left;" align="left"><h3>Password:</h3></div>
  45. <div style="width:300px;float:left;" align="left"><input type="password" name="passwd" id="passwd" value="" class="textAreaBoxInputs" placeholder="Password"></div><br clear="all"><br clear="all">
  46. <div style="width:115px; padding-top:10px;float:left;" align="left">&nbsp;</div>
  47. <div style="width:300px;float:left;" align="left">
  48. <input type="hidden" name="submitted" id="submitted" value="yes">
  49. <input type="submit" name="submit" id="" value="Login" style="margin-right:50px;" class="general_button">
  50. <a href="signup.php" style="text-decoration:none;" class="general_button">Register</a>
  51. </div>
  52. </form>
  53. <br clear="all">
  54. <br clear="all"><br clear="all">
  55. <div style="width:250px; font-family:Verdana, Geneva, sans-serif; font-size:11px;" align="left">
  56. <b>Account To Login For A Demonstration</b><br>
  57. Email Address: <i>[email protected]</i><br>
  58. Demo Password: <i>source123</i>
  59. </div>
  60. </div>
  61. </center>
  62. </center>
  63. </body>
  64. </html>
resultsignup.php - Signup.php is for creating a new account and storing the data in the database. Every account that the user created their passwords will automaticall encrypted using the MD5.
  1. <?php
  2. include "database_connection.php";
  3.  
  4. if(isset($_POST["submitted"]) && $_POST["submitted"] == "yes")
  5. {
  6. $firstname = trim(strip_tags($_POST['firstname']));
  7. $lastname = trim(strip_tags($_POST['lastname']));
  8. $user_email = trim(strip_tags($_POST['email']));
  9. $user_password = trim(strip_tags($_POST['passwd']));
  10. $encrypted_md5_password = md5($user_password);
  11. $check_for_duplicates = mysql_query("select * from `signup_and_login_table` where `email` = '".mysql_real_escape_string($user_email)."'");
  12.  
  13. if($firstname == "" || $lastname == "" || $user_email == "" || $user_password == "")
  14. {
  15. $error = '<br><div class="info">Sorry, all fields are required to create a new account. Thanks.</div><br>';
  16. }
  17. elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $user_email))
  18. {
  19. $error = '<br><div class="info">Sorry, Your email address is invalid, please enter a valid email address to proceed. Thanks.</div><br>';
  20. }
  21. else if(mysql_num_rows($check_for_duplicates) > 0)
  22. {
  23. $error = '<br><div class="info">Sorry, your email address already exist in our database and duplicate email addresses are not allowed for security reasons.<br>Please enter a different email address to proceed or login with your existing account. Thanks.</div><br>';
  24. }
  25. else
  26. {
  27. if(mysql_query("insert into `signup_and_login_table` values('', '".mysql_real_escape_string($firstname)."', '".mysql_real_escape_string($lastname)."', '".mysql_real_escape_string($user_email)."', '".mysql_real_escape_string($encrypted_md5_password)."', '".mysql_real_escape_string(date('d-m-Y'))."')"))
  28. {
  29. $_SESSION["VALID_USER_ID"] = $user_email;
  30. $_SESSION["USER_FULLNAME"] = strip_tags($firstname.'&nbsp;'.$lastname);
  31. header("location: index.php");
  32. }
  33. else
  34. {
  35. $error = '<br><div class="info">Sorry, your account could not be created at the moment. Please try again or contact the site admin to report this error if the problem persist. Thanks.</div><br>';
  36. }
  37. }
  38. }
  39. ?>
  40. <!DOCTYPE html>
  41. <html>
  42. <head>
  43. <title>Secure Login and Registration</title>
  44. <link href="css/style.css" rel="stylesheet" type="text/css">
  45. </head>
  46. <body>
  47. <center>
  48. <br /><br /><br /><div style="font-family:Verdana, Geneva, sans-serif; font-size:24px;"><h3>Secure Login and Registration</h3></div>
  49. <center>
  50. <div class="main_wrapper">
  51. <br clear="all">
  52. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  53. <h2 align="center" style="margin-top:0px;">Users Registration</h2><hr/><br />
  54. <div style="width:115px; padding-top:10px;float:left;" align="left"><b>Firstname:</b></div>
  55. <div style="width:300px;float:left;" align="left"><input type="text" name="firstname" id="firstname" value="" class="textAreaBoxInputs" placeholder="Your Firstname"></div><br clear="all"><br clear="all">
  56. <div style="width:115px; padding-top:10px;float:left;" align="left"><b>Lastname:</b></div>
  57. <div style="width:300px;float:left;" align="left"><input type="text" name="lastname" id="lastname" value="" class="textAreaBoxInputs" placeholder="Your Lastname"></div><br clear="all"><br clear="all">
  58. <div style="width:115px; padding-top:10px;float:left;" align="left"><b>Email Address:</b></div>
  59. <div style="width:300px;float:left;" align="left"><input type="text" name="email" id="email" value="" class="textAreaBoxInputs" placeholder="Email Address"></div><br clear="all"><br clear="all">
  60. <div style="width:115px; padding-top:10px;float:left;" align="left"><b>Password:</b></div>
  61. <div style="width:300px;float:left;" align="left"><input type="password" name="passwd" id="passwd" value="" class="textAreaBoxInputs" placeholder="Password"></div><br clear="all"><br clear="all">
  62. <div style="width:115px; padding-top:10px;float:left;" align="left">&nbsp;</div>
  63. <div style="width:300px;float:left;" align="left">
  64. <input type="hidden" name="submitted" id="submitted" value="yes">
  65. <input type="submit" name="submit" id="" value="Register" style="margin-right:50px;" class="general_button">
  66. <a href="login.php" style="text-decoration:none;" class="general_button">Back to Login</a>
  67. </div>
  68. </form>
  69. <br clear="all"><br clear="all">
  70. </div>
  71. </center>
  72. </center>
  73. </body>
  74. </html>
Hope that you learn in this tutorial and enjoy coding. Don't forget to LIKE & SHARE this website.

Add new comment