CodeIgniter Forums

Full Version: codeigneter sessions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]hamzakhan[/eluser]
i have 5 or 6 controllers.
should i put this code on each controller or is there is any short smart way
to put in one file and use every where.
//// login check
if ($this->session->userdata('logged_in') == FALSE)
{
redirect('admin/Login');
}

El Forum

[eluser]Ripe[/eluser]
You could create a hook but you would also need to tell it when not to redirect, like you wouldn't want it to redirect on the login page because that would create a loop.

http://ellislab.com/codeigniter/user-gui...hooks.html

My logged in hook looks like this:
Code:
function protected_pages()
{
    $CI =& get_instance();

    if (in_array(strtolower(get_class(get_instance())), (!$CI->user->is_logged_in()) ? option('offline_protected_pages') : option('online_protected_pages')))
        show_404();
}
Offline protected pages is an array, the same as online protected pages. I just show a show_404() page to prevent any looping ever.

El Forum

[eluser]├╝berfuzz[/eluser]
I see two alternatives.
1. Make an auth library with a method called redirect(). Make it look for and validate the user/session. Then you can use it where ever you want.
Code:
$this->load->library('Auth');
$this->Auth->redirect();
2. Make a MY_Controller that do the same stuff.
Code:
if(!$this->session->userdata('logged_in'))
{
   //rip off!
}

El Forum

[eluser]jedd[/eluser]
[quote author="hamzakhan" date="1259874075"]
should i put this code on each controller or is there is any short smart way
[/quote]

The really smart way is to read the [url="/wiki/FAQ"]FAQ[/url] and look for the question that says 'I keep repeating lumps of code, is there some other way ...'.

El Forum

[eluser]hamzakhan[/eluser]
thanks for your suggestion
------------
i am new in codeigneter so bit difficult to understand
can anyone give me proper example