Advertisement
ctwillie77

Login page

Aug 2nd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.06 KB | None | 0 0
  1. <?php
  2.     session_start();
  3. ?>
  4. <!DOCTYPE html>
  5. <html>
  6.  <head>
  7.     <meta charset="utf-8">
  8.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  9.     <meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  10.      <link rel="stylesheet" type="text/css" href="inv.css">
  11.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>  
  12.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  13. </head>
  14.    
  15. <body class="body_sign_in">
  16.    
  17.     <div class="container">
  18.         <div class="row">
  19.             <div class="col-xs-6 col-xs-offset-3">
  20.            
  21.                 <div class="well" id="sign_in_form">
  22.                
  23.                     <h1>Please Sign In</h1>
  24.                    
  25.                     <form action="#" method="post">
  26.                         <div class="form-group">
  27.                             <label for="username">Username:</label>
  28.                             <input class="form-control" type="text" name="username">
  29.                         </div>
  30.                        
  31.                         <div class="form-group">
  32.                             <label for="password">Password:</label>
  33.                             <input class="form-control" type="text" name="password">
  34.                         </div>
  35.                        
  36.                         <button class="btn btn-success sign_in_btn" type="submit" name="submit">Sign In</button>
  37.                    
  38.                     </form>
  39.                    
  40.                
  41.                 </div>
  42.                
  43.                
  44.             </div>
  45.         </div>
  46.     </div>
  47.    
  48.    
  49.    
  50.    
  51.    
  52.    
  53.     <!-- PHP Section -->
  54.    
  55.     <?php
  56.    
  57.     try {
  58.        
  59.         $host = "localhost";
  60.         $dbname = "inventory";
  61.         $username = "root";
  62.         $pwd = " ";
  63.        
  64.         $conn = new PDO("mysql:host=$host; dbname=$dbname", $username, $pwd);
  65.         // set the PDO error mode to exception
  66.         $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  67.         // echo "Connected Successfully!";
  68.     }
  69.     catch (PDOException $e) {
  70.         echo "Connection failed: " . $e->getMessage();
  71.     }
  72.    
  73.     if (isset($_POST['submit'])) {
  74.        
  75.         $username = $_POST['username'];
  76.         $password = $_POST['password'];
  77.        
  78.         //set Session username variable
  79.         $_SESSION['username'] = $username;
  80.          // echo "Session variable is set!";
  81.        
  82.         // Check if username field is set
  83.         if (empty($username)) {
  84.             echo "<div class='container'><div class='row'><div class='col-xs-6 col-xs-offset-3'>";
  85.             echo "<h3>Oops! Username is empty.</h3>";
  86.             echo "</div></div>";
  87.             exit();
  88.         }
  89.        
  90.         // Check if password field is set
  91.         if (empty($password)) {
  92.             echo "<div class='container'><div class='row'><div class='col-xs-6 col-xs-offset-3'>";
  93.             echo "<h3>Oops! Password is empty.</h3>";
  94.             echo "</div></div>";
  95.             exit();
  96.         }
  97.        
  98.        
  99.         // Query database for matching username and password
  100.         $sql = "SELECT * FROM login WHERE username = :username AND password = :password";
  101.         $statement = $conn->prepare($sql);
  102.         $statement->execute(array (':username' => $username,':password' => $password));
  103.         $count = $statement->rowCount();
  104.        
  105.         if ($count == 0) {
  106.             echo "<div class='container'><div class='row'><div class='col-xs-6 col-xs-offset-3'>";
  107.             echo "<h3>Sorry! Username and/or password is invalid. Try again!</h3>";
  108.             echo "</div></div>";
  109.             exit();
  110.         }
  111.         else if ($count > 0) {
  112.             header('location: http://localhost/Inventory/inv_home.php');
  113.         }
  114.        
  115.     }
  116.    
  117.    
  118.    
  119.     ?>
  120.    
  121.     <!-- PHP Section -->
  122.    
  123.    
  124.    
  125.    
  126.    
  127.    
  128.    
  129.    
  130.    
  131.    
  132.     </body>
  133. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement