/** * Login */ public function login() { if (isset($_POST['username']) && isset($_POST['password'])) { $user = new User(); $username = $_POST['username']; $password = $_POST['password']; $result = $user->doLogin($username, $password); if (false === $result) { return call('pages', 'error'); } else { $this->sessionCreate($result['username']); Session::set('is_logged_in', true); Session::set('userid', (int) $result['userid']); Session::set('user', $result['username']); Session::set('name', $result['firstname']); Session::set('surname', $result['lastname']); Session::set('generated_time', time()); header('location:?controller=pages&action=home'); } } else { if (Session::isLoggedIn()) { header('location:?controller=pages&action=home'); } else { require_once '../Views/users/login.php'; } } }
public function listusers() { $userModel = new User(); $users = $userModel->findAll(); $giftRequestService = new GiftRequestService(); $userGifts = $giftRequestService->getUsersGifts(); foreach ($users as $key => $user) { if ($user->id == Session::getUserId()) { unset($users[$key]); } } $giftModel = new Gift(); $gifts = $giftModel->findAll(); require_once '../Views/gifts/listusers.php'; }
break; case 'users': require_once '../Models/User.php'; $controller = new \Application\Controller\UsersController(); break; case 'gifts': require_once '../Models/User.php'; require_once '../Models/Gift.php'; require_once '../Models/GiftRequest.php'; $controller = new \Application\Controller\GiftsController(); break; } $controller->{$action}(); } $controllers = array('pages' => array('home', 'error'), 'users' => array('home', 'login', 'logout'), 'gifts' => array('home', 'send', 'listusers', 'accept')); $allows = array('pages' => array('home', 'error'), 'users' => array('login', 'logout'), 'gifts' => array()); if (array_key_exists($controller, $controllers)) { if (in_array($action, $controllers[$controller])) { if (in_array($action, $allows[$controller])) { call($controller, $action); } elseif (\Application\Helper\Session::isLoggedIn()) { call($controller, $action); } else { call('pages', 'home'); } } else { call('pages', 'error'); } } else { call('pages', 'error'); }
<?php if (\Application\Helper\Session::isLoggedIn()) { $session = \Application\Helper\Session::getSession(); echo '<p>Welcome to gift world <b>' . $session['name'] . ' ' . $session['surname'] . '</b></b></p>'; } else { ?> <p>Welcome to gift world ! Please <a href='?controller=users&action=login'>Login</a> to play.</p> <?php }
public function checkSession() { if (!Session::isLoggedIn()) { header('location:?controller=pages&action=home'); } }