Wednesday, 15 January 2014

php - how to logout from all views in codeigniter , am i using wrong code? -



php - how to logout from all views in codeigniter , am i using wrong code? -

hey guys help me logout pages of view..when click on logout link 1 page when trying logout page not work . . cpntoller code is:-

function index() { if($this->session->userdata('logged_in')) { $session_data = $this->session->userdata('logged_in'); $data['email'] = $session_data['email']; $this->load->view('home_view', $data); $this->load->view('home_content_view', $data); } else { //if no session, redirect login page redirect('login', 'refresh'); } } function logout() { $this->session->unset_userdata('logged_in'); session_destroy(); redirect('home', 'refresh'); }

first of codeigniter doesnot utilize php native session session_destroy won't work utilize $this->session->sess_destroy() instead. in constructor of each controller (if have multiple controllers). check user session if not exist redirect user page want display.

class home extends ci_controller { function __construct() { parent::__construct(); //this status checks existence of session if user not accessing //login method can accessed without user session if( !$this->session->userdata('logged_in') && $this->router->method != 'login' ) { redirect('login'); } } function index() { $session_data = $this->session->userdata('logged_in'); $data['email'] = $session_data['email']; $this->load->view('home_view', $data); $this->load->view('home_content_view', $data); } function logout() { $this->session->userdata = array(); $this->session->sess_destroy(); redirect('home', 'refresh'); } }

php mysql codeigniter

No comments:

Post a Comment