php - Session Management For redirection in Codeigniter -
i want manage page redirection in codeigniter, have 2 controllers:
- logggedin
- login
when user try access login
page while logged in, redirected loggedin
controller
function __construct() { parent::__construct(); $u = $this->session->userdata('username'); if(! isset($u)) { redirect('loggedin'); } }
and when tries access loggedin
controller while not logged in, should redirected login
controller
function __construct() { parent::__construct(); $u = $this->session->userdata('username'); if(isset($u)) { redirect('login'); } }
but when press logout button, has redirect login
controller, still remains on logggedin
controller.
function logout() { $this->session->sess_destroy(); redirect('login'); }
what problem in code?
your condition returning true
$u=$this->session->userdata('username'); // true because $u equal value session or false if(!isset($u)) { redirect('loggedin'); }
instead, should this
$u=$this->session->userdata('username'); if( ! $u) { redirect('loggedin'); }
the userdata
method return boolean(false)
if data not present.
from code igniter manual - retrieving session data
note: function returns false (boolean) if item trying access not exist.
Comments
Post a Comment