Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1. <?php
  2.     session_start();
  3.     function validate() {
  4.         if (empty($_POST['username'])) return false;
  5.         if (empty($_POST['password'])) return false;
  6.         if ($_POST['username'] != 'foo') return false;
  7.         if ($_POST['password'] != 'bar') return false;
  8.         // Otherwise all validation is passed user may be granted access
  9.         $_SESSION['auth'] = true;
  10.         return true;
  11.     }
  12.  
  13.     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  14.         if (!validate()) {
  15.             $msg = "User authentication failed!";
  16.         }
  17.         else {
  18.             $msg = "User authentication passed!";
  19.         }
  20.     }
  21.  
  22. if (!empty($msg)) {
  23. ?>
  24. <p><?php echo $msg; ?></p>
  25. <?php
  26. if (empty($_SESSION['auth'])) {
  27. ?>
  28. <form method="post">
  29. <p>Username: <input type="text" name="username" /></p>
  30. <p>Password: <input type="password" name="password" /></p>
  31. <input type="submit" value="Log In" />
  32. </form>
  33. <?php
  34. } else {
  35. ?>
  36. <h1>Welcome foo!</h1>
  37. <?php
  38. }
  39. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement