Advertisement
edvinasdd

Untitled

Aug 2nd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. <?php
  2. /*
  3. * Tutorial: PHP Login Registration system
  4. *
  5. * Page index.php
  6. * */
  7.  
  8. // Start Session
  9. session_start();
  10.  
  11. // Database connection
  12. require __DIR__ . '/database.php';
  13. $db = DB();
  14.  
  15. // Application library ( with DemoLib class )
  16. require __DIR__ . '/lib/library.php';
  17. $app = new DemoLib();
  18.  
  19. $login_error_message = '';
  20. $register_error_message = '';
  21.  
  22. // check Login request
  23. if (!empty($_POST['btnLogin'])) {
  24.  
  25. $username = trim($_POST['username']);
  26. $password = trim($_POST['password']);
  27.  
  28. if ($username == "") {
  29. $login_error_message = 'Username field is required!';
  30. } else if ($password == "") {
  31. $login_error_message = 'Password field is required!';
  32. } else {
  33. $user_id = $app->Login($username, $password); // check user login
  34. if($user_id > 0)
  35. {
  36. $_SESSION['user_id'] = $user_id; // Set Session
  37. header("Location: profile.php"); // Redirect user to the profile.php
  38. }
  39. else
  40. {
  41. $login_error_message = 'Invalid login details!';
  42. }
  43. }
  44. }
  45.  
  46. // check Register request
  47. if (!empty($_POST['btnRegister'])) {
  48. if ($_POST['name'] == "") {
  49. $register_error_message = 'Name field is required!';
  50. } else if ($_POST['email'] == "") {
  51. $register_error_message = 'Email field is required!';
  52. } else if ($_POST['username'] == "") {
  53. $register_error_message = 'Username field is required!';
  54. } else if ($_POST['password'] == "") {
  55. $register_error_message = 'Password field is required!';
  56. } else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  57. $register_error_message = 'Invalid email address!';
  58. } else if ($app->isEmail($_POST['email'])) {
  59. $register_error_message = 'Email is already in use!';
  60. } else if ($app->isUsername($_POST['username'])) {
  61. $register_error_message = 'Username is already in use!';
  62. } else {
  63. $user_id = $app->Register($_POST['name'], $_POST['email'], $_POST['username'], $_POST['password']);
  64. // set session and redirect user to the profile page
  65. $_SESSION['user_id'] = $user_id;
  66. header("Location: profile.php");
  67. }
  68. }
  69. ?>
  70. <!doctype html>
  71. <html lang="en">
  72. <head>
  73. <meta charset="UTF-8">
  74. <title>Home</title>
  75. <!-- Latest compiled and minified CSS -->
  76. <link rel="stylesheet" href="css/bootstrap.min.css">
  77. </head>
  78. <body>
  79.  
  80. <div class="container">
  81. <div class="row">
  82. <div class="col-md-12">
  83. <h2>
  84. PHP Login Registration System with PDO Connection using SHA-256 Cryptographic Hash Algorithm to store Password
  85. </h2>
  86. </div>
  87. </div>
  88. <div class="form-group">
  89. Note: This is demo version from iTech Empires tutorials.
  90. </div>
  91. <div class="row">
  92. <div class="col-md-5 well">
  93. <h4>Register</h4>
  94. <?php
  95. if ($register_error_message != "") {
  96. echo '<div class="alert alert-danger"><strong>Error: </strong> ' . $register_error_message . '</div>';
  97. }
  98. ?>
  99. <form action="index.php" method="post">
  100. <div class="form-group">
  101. <label for="">Name</label>
  102. <input type="text" name="name" class="form-control"/>
  103. </div>
  104. <div class="form-group">
  105. <label for="">Email</label>
  106. <input type="email" name="email" class="form-control"/>
  107. </div>
  108. <div class="form-group">
  109. <label for="">Username</label>
  110. <input type="text" name="username" class="form-control"/>
  111. </div>
  112. <div class="form-group">
  113. <label for="">Password</label>
  114. <input type="password" name="password" class="form-control"/>
  115. </div>
  116. <div class="form-group">
  117. <input type="submit" name="btnRegister" class="btn btn-primary" value="Register"/>
  118. </div>
  119. </form>
  120. </div>
  121. <div class="col-md-2"></div>
  122. <div class="col-md-5 well">
  123. <h4>Login</h4>
  124. <?php
  125. if ($login_error_message != "") {
  126. echo '<div class="alert alert-danger"><strong>Error: </strong> ' . $login_error_message . '</div>';
  127. }
  128.  
  129. function check_password_db($nickname,$password) {
  130. // Here u have to include your DB connection and select!
  131. $a=mysql_query("SELECT password FROM users where username = '$username'");
  132. if(mysql_num_rows($a) == 1 ) {
  133. $password_info=mysql_fetch_array($a);
  134. $sha_info = explode("$",$password_info[0]);
  135. } else
  136. return false;
  137. if( $sha_info[1] === "SHA" ) {
  138. $salt = $sha_info[2];
  139. $sha256_password = hash('sha256', $password);
  140. $sha256_password .= $sha_info[2];;
  141. if( strcasecmp(trim($sha_info[3]),hash('sha256', $sha256_password) ) == 0 )
  142. return true;
  143. else return false;
  144. }
  145. }
  146.  
  147. ?>
  148. <form action="index.php" method="post">
  149. <div class="form-group">
  150. <label for="">Username/Email</label>
  151. <input type="text" name="username" class="form-control"/>
  152. </div>
  153. <div class="form-group">
  154. <label for="">Password</label>
  155. <input type="password" name="password" class="form-control"/>
  156. </div>
  157. <div class="form-group">
  158. <input type="submit" name="btnLogin" class="btn btn-primary" value="Login"/>
  159. </div>
  160. </form>
  161. </div>
  162. </div>
  163. </div>
  164.  
  165. </body>
  166. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement