In my home
controller I'm checking whether the user is logged in or not. See the code below:
<?php
class Home extends Controller {
private $db;
private $session;
public function __construct($db, $session) {
$this->db = $db;
$this->session = $session;
parent::__construct($db, $session);
}
public function index() {
$this->view('home/index', array('user' => $x ) );
}
public function login() {
if( $this->session->loggedIn == true ) {
$this->index();
exit();
}
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$sessionToDb = $this->session->startSession( $_POST['user_email'], $_POST['user_password'] );
if( $sessionToDb === true ) {
$this->index();
exit();
}
else {
$data['error'] = '<p>Fout tijdens inloggen.<br />'.$x.'</p>';
}
}
$data['url'] = $_SERVER['REQUEST_URI'];
$this->view('home/login', $data);
}
public function logout() {
$this->session->endSession();
$this->login();
exit();
}
}
?>
If the user is already logged in, he should be able to see the login screen, so I added this code:
public function login() {
if( $this->session->loggedIn == true ) {
$this->index();
exit();
}
Which works... however the URL stays the same: admin/home/login/
, which could be confusing.
So alternatively I could redirect the user:
public function login() {
if( $this->session->loggedIn == true ) {
header('Location: /admin/home/index/';
exit();
}
Same goes for the logout()
function.
Which is the better approach?