Advertisement
tjromano

Registration

Sep 9th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.91 KB | None | 0 0
  1. <?php
  2. /*
  3.  * This is a PHP Secure login system.
  4.  *
  5.  */
  6. //require user configuration and database connection parameters
  7. require('config.php');
  8.  
  9. //pre-define validation parameters
  10.  
  11. $usernamenotempty=TRUE;
  12. $usernamevalidate=TRUE;
  13. $usernamenotduplicate=TRUE;
  14. $passwordnotempty=TRUE;
  15. $passwordmatch=TRUE;
  16. $passwordvalidate=TRUE;
  17.  
  18. //Check if user submitted the desired password and username
  19. if ((isset($_POST["desired_password"])) && (isset($_POST["desired_username"])) && (isset($_POST["desired_password1"])))  {
  20.    
  21. //Username and Password has been submitted by the user
  22. //Receive and validate the submitted information
  23. //sanitize user inputs
  24.  
  25. function sanitize($data){
  26. $data=trim($data);
  27. $data=htmlspecialchars($data);
  28. $data=mysql_real_escape_string($data);
  29. return $data;
  30. }
  31.  
  32. $desired_username=sanitize($_POST["desired_username"]);
  33. $desired_password=sanitize($_POST["desired_password"]);
  34. $desired_password1=sanitize($_POST["desired_password1"]);
  35.  
  36. //validate username
  37.  
  38. if (empty($desired_username)) {
  39. $usernamenotempty=FALSE;
  40. } else {
  41. $usernamenotempty=TRUE;
  42. }
  43.  
  44. if ((!(ctype_alnum($desired_username))) || ((strlen($desired_username)) >11)) {
  45. $usernamevalidate=FALSE;
  46. } else {
  47. $usernamevalidate=TRUE;
  48. }
  49.  
  50. if (!($fetch = mysql_fetch_array( mysql_query("SELECT `username` FROM `authentication` WHERE `username`='$desired_username'")))) {
  51. //no records for this user in the MySQL database
  52. $usernamenotduplicate=TRUE;
  53. }
  54. else {
  55. $usernamenotduplicate=FALSE;
  56. }
  57.  
  58. //validate password
  59.  
  60. if (empty($desired_password)) {
  61. $passwordnotempty=FALSE;
  62. } else {
  63. $passwordnotempty=TRUE;
  64. }
  65.  
  66. if ((!(ctype_alnum($desired_password))) || ((strlen($desired_password)) < 8)) {
  67. $passwordvalidate=FALSE;
  68. } else {
  69. $passwordvalidate=TRUE;
  70. }
  71.  
  72. if ($desired_password==$desired_password1) {
  73. $passwordmatch=TRUE;
  74. } else {
  75. $passwordmatch=FALSE;
  76. }
  77.  
  78. if (($usernamenotempty==TRUE)
  79. && ($usernamevalidate==TRUE)
  80. && ($usernamenotduplicate==TRUE)
  81. && ($passwordnotempty==TRUE)
  82. && ($passwordmatch==TRUE)
  83. && ($passwordvalidate==TRUE)) {
  84. //The username and password validation succeeds.
  85.  
  86. //Hash the password
  87. //This is very important for security reasons because once the password has been compromised,
  88. //The attacker cannot still get the plain text password equivalent without brute force.
  89.  
  90. function HashPassword($input)
  91. {
  92. //Credits: http://crackstation.net/hashing-security.html
  93. //This is secure hashing the consist of strong hash algorithm sha 256 and using highly random salt
  94. $salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
  95. $hash = hash("sha256", $salt . $input);
  96. $final = $salt . $hash;
  97. return $final;
  98. }
  99.  
  100. $hashedpassword= HashPassword($desired_password);
  101.  
  102. //Insert username and the hashed password to MySQL database
  103.  
  104. mysql_query("INSERT INTO `authentication` (`username`, `password`) VALUES ('$desired_username', '$hashedpassword')") or die(mysql_error());
  105. //Send notification to webmaster
  106. $message = "New member has just registered: $desired_username";
  107. mail($email, $subject, $message, $from);
  108. //redirect to login page
  109. header(sprintf("Location: %s", $loginpage_url));   
  110. exit;
  111. }
  112. }
  113. ?>
  114. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  115. <html xmlns="http://www.w3.org/1999/xhtml">
  116. <head>
  117. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  118. <title>Leads Dashboard</title>
  119. <link href="css/style.css" rel="stylesheet" type="text/css" />
  120. </head>
  121.  
  122. <body>
  123. <div id="head-container">
  124.   <div id="header">
  125.     <h1><img src="Images/cl_header.png" width="1280" height="150" /></h1>
  126.   </div>
  127. </div>
  128. <div id="content-container">
  129.   <div id="content-container2">
  130.     <div id="content-container3">
  131.       <div id="content">
  132.         <h1>Reports Dashboard</h1>
  133. <!-- Start of registration form -->
  134. <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
  135. Username: (<i>alphanumeric less than 12 characters</i>) <input type="text" class="<?php if (($usernamenotempty==FALSE) || ($usernamevalidate==FALSE) || ($usernamenotduplicate==FALSE))  echo "invalid"; ?>" id="desired_username" name="desired_username"><br /><br />
  136. Password: (<i>alphanumeric greater than 8 characters</i>) <input name="desired_password" type="password" class="<?php if (($passwordnotempty==FALSE) || ($passwordmatch==FALSE) || ($passwordvalidate==FALSE)) echo "invalid"; ?>" id="desired_password" ><br /><br />
  137. Type the password again: <input name="desired_password1" type="password" class="<?php if (($passwordnotempty==FALSE) || ($passwordmatch==FALSE) || ($passwordvalidate==FALSE)) echo "invalid"; ?>" id="desired_password1" ><br />
  138. <br /><br />
  139. <input type="submit" value="Register">
  140. <br /><br />
  141. <a href="index.php">Back to Homepage</a><br />
  142. <!-- Display validation errors -->
  143. <?php if ($usernamenotempty==FALSE) echo '<font color="red">You have entered an empty username.</font>'; ?><br />
  144. <?php if ($usernamevalidate==FALSE) echo '<font color="red">Your username should be alphanumeric and less than 12 characters.</font>'; ?><br />
  145. <?php if ($usernamenotduplicate==FALSE) echo '<font color="red">Please choose another username, your username is already used.</font>'; ?><br />
  146. <?php if ($passwordnotempty==FALSE) echo '<font color="red">Your password is empty.</font>'; ?><br />
  147. <?php if ($passwordmatch==FALSE) echo '<font color="red">Your password does not match.</font>'; ?><br />
  148. <?php if ($passwordvalidate==FALSE) echo '<font color="red">Your password should be alphanumeric and greater 8 characters.</font>'; ?><br />
  149. <br />
  150. </form>
  151. <!-- End of registration form -->
  152. <p>&nbsp;</p>
  153.       </div>
  154.     </div>
  155.   </div>
  156. </div>
  157. <div id="footer-container">
  158.   <div id="footer">
  159.     <div align="center">Mortgage Master Inc.
  160.       409 Canal Street, Plantsville, CT 06479 • 860-716-1091 • CLang@mortgagemasterinc.com <br/>
  161.       website powered by<br/>
  162.       <a href="bkup/www.mbcinteractive.com">MBC Interactive</a></div>
  163.   </div>
  164.   </div>
  165. </body>
  166. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement