When user logins or when he registers, his $_SESSION['username'] gets the value of his username. I want to allow them access certain controllers only if they registered. But I don’t like the idea of writing this:
function __construct()
{
session_start();
if(empty($_SESSION['username']))
die();
parent::__construct();
}
in every controller I don’t want unloggined people to see. Is there any better way to do it? Thanks.
You might consider extending your base controller class (see the Codeigniter Wiki Article on extending the class) to provide a
require_loginfunction:Once you have this, you can call
require_login()in the constructor of each controller you want to protect:Finally, instead of using PHP’s
$_SESSIONsuperglobal for session storage, you might consider taking advantage of the Codeignitersessionlibrary or an equivalent.