Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require './Model/Account.php';
- use UserAccounts\Model\Account;
- if (session_status() == PHP_SESSION_NONE) {
- session_start();
- }
- $showRegisterSuccess = false;
- $errorMessage = null;
- // is the user already logged in?
- if (isset($_SESSION['account'])) {
- // yes, found in session, so redirect
- header('Location: admin.php');
- } elseif (isset($_COOKIE['account'])) {
- // yes, found in cookie. Copy to session
- // and redirect
- $_SESSION['account'] = unserialize($_COOKIE['account']);
- header('Location: admin.php');
- }
- $action = null;
- if (isset($_GET['action'])) {
- $action = $_GET['action'];
- }
- switch($action) {
- case "signIn": checkLogin(); break;
- case "registered": $showRegisterSuccess = true; break;
- case "signOut": logout(); break;
- }
- function checkLogin() {
- global $errorMessage;
- // get form data
- $username = $_POST['inputEmail'];
- $password = $_POST['inputPassword'];
- $remember = isset($_POST['remember-me']);
- // try to log in
- try {
- $account = Account::getAccount($username, $password);
- } catch (Exception $ex) {
- $errorMessage = "Internal Error :( <br>";
- }
- if ($account) {
- // Successful login. Add to session
- $_SESSION['account'] = $account;
- if ($remember) {
- // User wants to be remembered. Set a cookie.
- setcookie('account', serialize($account),
- time() + (86400 * 30), '/'); // 1 month
- }
- header('Location: admin.php');
- } else {
- $errorMessage = "Incorrect username or password.";
- }
- }
- function logout() {
- $_SESSION['account'] = null;
- session_destroy();
- setcookie('account', null, -1, '/');
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
- <title>User Accounts</title>
- <?php require 'Common/header.php'; ?> <!-- Bootstrap files -->
- </head>
- <body>
- <!-- Navigation bar -->
- <?php require 'Common/nav.php'; ?>
- <div class="container">
- <?php
- if ($errorMessage != null) {
- echo "<div class='alert alert-danger' role='alert'>";
- echo $errorMessage;
- echo "</div>";
- }
- if ($showRegisterSuccess) {
- echo "<div class='alert alert-success' role='alert'>";
- echo "Account registered :) Please sign in below.";
- echo "</div>";
- }
- ?>
- <form class="form-signin" method="POST" action="index.php?action=signIn">
- <h2 class="form-signin-heading">Please sign in</h2>
- <label for="inputEmail" class="sr-only">Email address</label>
- <input type="email" id="inputEmail" name="inputEmail" class="form-control" placeholder="Email address" required autofocus>
- <label for="inputPassword" class="sr-only">Password</label>
- <input type="password" id="inputPassword" name="inputPassword" class="form-control" placeholder="Password" required>
- <div class="checkbox">
- <label>
- <input type="checkbox" name="remember-me" value="remember-me"> Remember me
- </label>
- </div>
- <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
- </form>
- <form action="register.php" class="form-signin">
- <button class="btn btn-lg btn-block btn-secondary" type="submit">Register</button>
- </form>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement