Advertisement
Guest User

Login Form created by Andrew Daniels

a guest
Aug 15th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. $conn = mysqli_connect("localhost", "root", "", "members");
  5.  
  6. if(!$conn) {
  7.     die("Oops! Something went wrong. Please try again later...");
  8. }
  9.  
  10. $error = false;
  11. if($_SERVER["REQUEST_METHOD"] == "POST") {
  12.     if(empty($_POST["username"])) {
  13.         $error = true;
  14.         echo "<h2 style='color:red; font-family:bold;'>Please enter your Username!</h2>";
  15.     } else {
  16.         $error = false;
  17.     }
  18.  
  19.     if(empty($_POST["password"])) {
  20.         $error = true;
  21.         echo "<h2 style='color:red; font-family:bold;'>Please enter your Password!</h2>";
  22.     } else {
  23.         $error = false;
  24.     }
  25.  
  26.     if(!$error && $_POST["username"] != "" && $_POST["password"] != "") {
  27.         $pass = trim($_POST["password"]);
  28.         $pass = strip_tags($pass);
  29.         $pass = htmlspecialchars($pass);
  30.  
  31.         $password = hash("sha256", $pass);
  32.  
  33.         $username = trim($_POST["username"]);
  34.         $username = strip_tags($username);
  35.         $username = htmlspecialchars($username);
  36.  
  37.         $sql = "SELECT * FROM users WHERE username = '$username'";
  38.  
  39.         $result = mysqli_query($conn, $sql);
  40.  
  41.         $row = mysqli_fetch_array($result);
  42.  
  43.         $rowcount = mysqli_num_rows($result);
  44.  
  45.         if ($rowcount == 1 && $password == $row["password"]) {
  46.             $_SESSION["id"] = $row["id"];
  47.             $_SESSION["username"];
  48.  
  49.             header("location: home.php");
  50.         }
  51.     }
  52. }
  53. ?>
  54.  
  55. <!DOCTYPE html>
  56. <html>
  57. <head>
  58.     <meta charset="UTF=8"/>
  59.     <meta name="viewport" content="width=device-width"/>
  60.     <title>Login System</title>
  61.     <link href="loginstyles.css"/>
  62. </head>
  63. <body>
  64.     <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
  65.         <span id="loginHeader">Login</span>
  66.         <br><br>
  67.         <input type="text" name="username" placeholder="Username"/>
  68.         <br>
  69.         <input type="password" name="password" placeholder="Password"/>
  70.         <br>
  71.         <button type="submit" name="submit" class="btn-primary">Login</button>
  72.     </form>
  73. </body>
  74. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement