Guest User

Untitled

a guest
Jan 21st, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. <form method="post" action="php/login.php">
  2. <div class="form-group">
  3. <input class="form-control" type="text" name="username" placeholder="Username" required>
  4. </div>
  5.  
  6. <div class="form-group">
  7. <input class="form-control" type="password" name="password" placeholder="Password" required>
  8. </div>
  9.  
  10. <div class="form-group">
  11. <input class="btn btn-primary" type="submit" name="login" value="Login">
  12. </div>
  13. </form>
  14.  
  15. <?php
  16. require_once "../functions.php";
  17.  
  18. db_connect();
  19.  
  20. $sql = "SELECT id, username, password FROM users WHERE username = ?";
  21. $statement = $conn->prepare($sql);
  22. $statement->bind_param('s', $_POST['username']);
  23. $statement->execute();
  24. $statement->store_result();
  25. $statement->bind_result($id, $username, $password);
  26. $statement->fetch();
  27.  
  28.  
  29. if ($statement->execute()) {
  30. if(password_verify($_POST['password'], $password)) {
  31. $_SESSION['user_id'] = $id;
  32. $_SESSION['user_username'] = $username;
  33. redirect_to("/home.php");
  34. } else {
  35. redirect_to("../index.php?login_error=true");
  36. }
  37. } else {
  38. echo "Error: " . $conn->error;
  39. }
  40.  
  41. <?php
  42. session_start();
  43.  
  44. function db_connect() {
  45. global $conn; // db connection variable
  46. $db_server = "localhost";
  47. $username = "root";
  48. $password = "xxxxxxxxx";
  49. $db_name = "xxxxxxxxxx";
  50.  
  51. // create a connection
  52. $conn = new mysqli($db_server, $username, $password, $db_name);
  53.  
  54. // check connection for errors
  55. if ($conn->connect_error) {
  56. die("Error: " . $conn->connect_error);
  57.  
  58. }
  59.  
  60. }
  61.  
  62. function redirect_to($url) {
  63. header("Location: " . $url);
  64. exit();
  65. }
  66.  
  67. function is_auth() {
  68. return isset($_SESSION['user_id']);
  69. }
  70.  
  71. function check_auth() {
  72. if(!is_auth()) {
  73. redirect_to("../index.php?logged_in=false");
  74. }
  75. }
  76.  
  77. if ($statement->execute()) {
  78. if (password_verify($_POST['password'], $password)) {
  79. $_SESSION['user_id'] = $id;
  80. $_SESSION['user_username'] = $username;
  81. redirect_to("/home.php");
  82. } else {
  83. redirect_to("../index.php?login_error=true");
  84. }
  85. } else {
  86. echo "Error: " . $conn->error;
  87. }
  88.  
  89. if (password_verify($_POST['password'], $password)) {
Add Comment
Please, Sign In to add comment