0

hey guys help me to logout from all pages of view..when i click on logout link i just from only one page when i am trying to logout from another page its not work . . my 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 to login page
            redirect('login', 'refresh');
        }
    }

    function logout()
    {
        $this->session->unset_userdata('logged_in');
        session_destroy();
        redirect('home', 'refresh');
    }
Jay
  • 392
  • 4
  • 10
  • 28

5 Answers5

1

First of all codeigniter doesnot use php native session so session_destroy won't work use $this->session->sess_destroy() instead. In the constructor of each controller (if you have multiple controllers). Check the user session if it does not exist then redirect user to page you want to display.

class Home extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        //this condition checks the existence of session if user is not accessing  
        //login method as it can be 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');
    }
}
Code Prank
  • 4,209
  • 5
  • 31
  • 47
  • 2
    +1 An even higher level of session control can be achieved if the OP has all their controllers inherit from a common `MY_Controller` base class. If the session check is implemented in the constructor of this base controller, then they can be done with it with a single call :) Please see a [previous answer of mine](http://stackoverflow.com/a/12486869/568884). – Jordan Arsenault Feb 11 '13 at 06:23
  • yes you are right... also codeigniter have a support for hooks you can also manage things using hooks. – Code Prank Feb 11 '13 at 06:26
  • Please post your entire controller file here – Code Prank Feb 11 '13 at 07:26
  • @ShayanHusaini class Home extends CI_Controller { function __construct() { parent::__construct(); } 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 to login page redirect('login', 'refresh'); } } function logout() { $this->session->unset_userdata('logged_in'); session_destroy(); redirect('home', 'refresh'); } – Jay Feb 11 '13 at 09:38
  • If thats all your controller contains then in which function its not working because there is only one function named index which loads the view – Code Prank Feb 11 '13 at 09:42
  • it working for only one view ,, i have more then one view and i want that it work from all @ShayanHusaini – Jay Feb 11 '13 at 09:56
1

I would recommend you to read about the _remap() function in CodeIgniter. What I usually do for login/logout is to override the remap in my custom controller that extends the CI_Controller and do like:

function _remap($method)
{
    if (method_exists($this, $method) && $this-my_custom_helper->is_logged_in())
    {
        $this->$method();
    }
    else
    {
        redirect('/auth/login/');
    }
}

That way you don't need to check if loggedin in each controller. Another thing that is useful is to implement a before/after filters that run before your actions in each controller. I would not go in detail, but you can check out the MY_Controller by Jamie Rumbelow found here. There are some nice techniques you can adopt from his code. Good luck!!

greenLizard
  • 2,326
  • 5
  • 24
  • 30
0

put below method in constructor __construct of each controller.

I assume you have login method on user controller's index function

  if (FALSE !== $this->session->userdata('logged_in')) {
       redirect('/user'); 
  }
xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • i want that the logout function work from all pages..its only work from one page , not from all – Jay Feb 11 '13 at 05:43
0

When user click the logout button that cal will go to logout function in controller file.

In that function

 public function logout() {
       $this->session->session_destroy();
       redirect("home");
 }
Venkata Krishna
  • 4,287
  • 6
  • 30
  • 53
  • i want that the logout function work from all pages..its only work from one page , not from all – Jay Feb 11 '13 at 05:40
  • How many controllers you have totally in your application and after you call function go to the pages where your sessions still valid and find session data and print it – Venkata Krishna Feb 11 '13 at 05:42
0

Try This

 function logout()
        {
            $this->session->unset_userdata($session_data); 
            $this->session->sess_destroy();
            redirect('home', 'refresh');
        }
Rahul Chipad
  • 2,373
  • 17
  • 20