Can anyone suggest is this best way to handle session in PHP MVC. I am checking the session inside the constructor of the controller. I think it will be executed eachtime the class will be called. Is this the correct way to do this or should I check for the session value in each method?
<?php
class Home extends Controller
{
protected $session;
public function __construct()
{
$session = $this->model('session');
if(!($session->getLoggedIn()))
{
//echo "Home/index/if";
header('Location: /login');
exit;
}
}
public function index($name = '')
{
//echo "Home/index";
$this->view('home', ['userid' => $session->getUserId(), 'username' => $session->getUserName() ]);
}
}