Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. <?php
  2. require './Model/Account.php';
  3. use UserAccounts\Model\Account;
  4.  
  5. if (session_status() == PHP_SESSION_NONE) {
  6. session_start();
  7. }
  8.  
  9. $showRegisterSuccess = false;
  10. $errorMessage = null;
  11.  
  12. // is the user already logged in?
  13. if (isset($_SESSION['account'])) {
  14. // yes, found in session, so redirect
  15. header('Location: admin.php');
  16. } elseif (isset($_COOKIE['account'])) {
  17. // yes, found in cookie. Copy to session
  18. // and redirect
  19. $_SESSION['account'] = unserialize($_COOKIE['account']);
  20. header('Location: admin.php');
  21. }
  22.  
  23. $action = null;
  24. if (isset($_GET['action'])) {
  25. $action = $_GET['action'];
  26. }
  27.  
  28. switch($action) {
  29. case "signIn": checkLogin(); break;
  30. case "registered": $showRegisterSuccess = true; break;
  31. case "signOut": logout(); break;
  32. }
  33.  
  34. function checkLogin() {
  35. global $errorMessage;
  36.  
  37. // get form data
  38. $username = $_POST['inputEmail'];
  39. $password = $_POST['inputPassword'];
  40. $remember = isset($_POST['remember-me']);
  41.  
  42. // try to log in
  43. try {
  44. $account = Account::getAccount($username, $password);
  45. } catch (Exception $ex) {
  46. $errorMessage = "Internal Error :( <br>";
  47. }
  48.  
  49. if ($account) {
  50. // Successful login. Add to session
  51. $_SESSION['account'] = $account;
  52. if ($remember) {
  53. // User wants to be remembered. Set a cookie.
  54. setcookie('account', serialize($account),
  55. time() + (86400 * 30), '/'); // 1 month
  56. }
  57. header('Location: admin.php');
  58. } else {
  59. $errorMessage = "Incorrect username or password.";
  60. }
  61. }
  62.  
  63. function logout() {
  64. $_SESSION['account'] = null;
  65. session_destroy();
  66. setcookie('account', null, -1, '/');
  67. }
  68.  
  69. ?>
  70.  
  71.  
  72. <!DOCTYPE html>
  73. <html>
  74. <head>
  75. <meta charset="utf-8">
  76. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  77. <title>User Accounts</title>
  78. <?php require 'Common/header.php'; ?> <!-- Bootstrap files -->
  79. </head>
  80. <body>
  81. <!-- Navigation bar -->
  82. <?php require 'Common/nav.php'; ?>
  83. <div class="container">
  84.  
  85. <?php
  86. if ($errorMessage != null) {
  87. echo "<div class='alert alert-danger' role='alert'>";
  88. echo $errorMessage;
  89. echo "</div>";
  90. }
  91.  
  92. if ($showRegisterSuccess) {
  93. echo "<div class='alert alert-success' role='alert'>";
  94. echo "Account registered :) Please sign in below.";
  95. echo "</div>";
  96. }
  97. ?>
  98.  
  99. <form class="form-signin" method="POST" action="index.php?action=signIn">
  100. <h2 class="form-signin-heading">Please sign in</h2>
  101. <label for="inputEmail" class="sr-only">Email address</label>
  102. <input type="email" id="inputEmail" name="inputEmail" class="form-control" placeholder="Email address" required autofocus>
  103. <label for="inputPassword" class="sr-only">Password</label>
  104. <input type="password" id="inputPassword" name="inputPassword" class="form-control" placeholder="Password" required>
  105. <div class="checkbox">
  106. <label>
  107. <input type="checkbox" name="remember-me" value="remember-me"> Remember me
  108. </label>
  109. </div>
  110. <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
  111. </form>
  112. <form action="register.php" class="form-signin">
  113. <button class="btn btn-lg btn-block btn-secondary" type="submit">Register</button>
  114. </form>
  115. </div>
  116. </body>
  117. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement