Guest User

Untitled

a guest
Apr 17th, 2017
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. <?php
  2. session_start();
  3. $_SESSION['loggedIn'] = true;
  4. function is_logged() {
  5. if (isset($_SESSION['username'])) return $_SESSION['username'];
  6. else return false;
  7. }
  8.  
  9. if( !isset($_SESSION['loggedIn']) && ($_SESSION['loggedIn'] != true) )
  10. {
  11. // redirect the user to login screen if the session variable is not set and its value is not true
  12. header('location: login.php');
  13. }
  14.  
  15. ?>
  16.  
  17.  
  18.  
  19. <html>
  20. <head>
  21. <title>Login</title>
  22. </head>
  23. <body>
  24. <?php
  25. if (!isset($_POST['submit'])){
  26. ?>
  27. <!-- The HTML login form -->
  28. <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
  29. Username: <input type="text" name="username" /><br />
  30. Password: <input type="password" name="password" /><br />
  31.  
  32. <input type="submit" name="submit" value="Login" />
  33. </form>
  34. <?php
  35. } else {
  36. require_once("db_const.php");
  37. $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  38. # check connection
  39. if ($mysqli->connect_errno) {
  40. echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
  41. exit();
  42. }
  43.  
  44. $username = $_POST['username'];
  45. $password = $_POST['password'];
  46.  
  47. $sql = "SELECT * from GEBRUIKERS WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
  48. $result = $mysqli->query($sql);
  49. if (!$result->num_rows == 1) {
  50. echo "<p>Invalid username/password combination</p>";
  51. } else {
  52. echo "<p>Logged in successfully</p>";
  53. // do stuffs
  54. }
  55.  
  56.  
  57.  
  58. if (mysqli_num_rows($result) > 0) {
  59. // Output data of each row
  60. while($row = mysqli_fetch_assoc($result)) {
  61. $_SESSION['+login_user']=$user; // Initializing Session
  62. header("location: welcome.php"); // Redirecting To Other Page
  63. }
  64. }
  65.  
  66.  
  67. else {
  68. $error = "Username or Password is invalid";
  69. }
  70.  
  71. mysqli_close($conn); // Closing Connection
  72.  
  73. }
  74. ?>
  75. </body>
  76. </html>
Add Comment
Please, Sign In to add comment