Advertisement
Guest User

code - login.php

a guest
Mar 21st, 2017
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.61 KB | None | 0 0
  1. <?php  //Start the Session
  2. session_start();
  3.  require('connect.php');
  4. //3. If the form is submitted or not.
  5. //3.1 If the form is submitted
  6. if (isset($_POST['username']) and isset($_POST['password'])){
  7. //3.1.1 Assigning posted values to variables.
  8. $username = $_POST['username'];
  9. $password = $_POST['password'];
  10. //3.1.2 Checking the values are existing in the database or not
  11. $query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
  12.  
  13. $result = mysqli_query($connection, $query) or die(mysqli_error($connection));
  14. $count = mysqli_num_rows($result);
  15. //3.1.2 If the posted values are equal to the database values, then session will be created for the user.
  16. if ($count == 1){
  17. $_SESSION['username'] = $username;
  18. }else{
  19. //3.1.3 If the login credentials doesn't match, he will be shown with an error message.
  20. $fmsg = "Invalid Login Credentials.";
  21. }
  22. }
  23. //3.1.4 if the user is logged in Greets the user with message
  24. if (isset($_SESSION['username'])){
  25. $username = $_SESSION['username'];
  26. echo "Hai " . $username . "
  27. ";
  28. echo "This is the Members Area
  29. ";
  30. echo "<a href='logout.php'>Logout</a>";
  31.  
  32. }else{
  33. //3.2 When the user visits the page first time, simple login form will be displayed.
  34. ?>
  35. <html>
  36. <head>
  37.     <title>User Login Using PHP & MySQL</title>
  38.    
  39. <!-- Latest compiled and minified CSS -->
  40. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
  41.  
  42. <!-- Optional theme -->
  43. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >
  44.  
  45. <link rel="stylesheet" href="styles.css" >
  46.  
  47. <!-- Latest compiled and minified JavaScript -->
  48. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  49. </head>
  50. <body>
  51.  
  52. <div class="container">
  53.       <form class="form-signin" method="POST">
  54.       <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
  55.         <h2 class="form-signin-heading">Please Login</h2>
  56.         <div class="input-group">
  57.       <span class="input-group-addon" id="basic-addon1">@</span>
  58.       <input type="text" name="username" class="form-control" placeholder="Username" required>
  59.     </div>
  60.         <label for="inputPassword" class="sr-only">Password</label>
  61.         <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
  62.         <button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
  63.         <a class="btn btn-lg btn-primary btn-block" href="register.php">Register</a>
  64.       </form>
  65. </div>
  66.  
  67. </body>
  68.  
  69. </html>
  70. <?php } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement