CodeIgniter Forums
Best practice in getting session information to be used by many methods - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Best practice in getting session information to be used by many methods (/showthread.php?tid=47057)



Best practice in getting session information to be used by many methods - El Forum - 11-24-2011

[eluser]Unknown[/eluser]
Hi people,

Let say at my controller named Book, I have many methods, such as get_book(), read_book(), remove_book(), etc.

No methods in the class can be used without user logged in, and I can get the user_id from session.

My question is, what is/are the best ways to check the if the user_id session is set so that I can use the methods?

As for now I am thinking of creating a is_logged_in() method, and apply it to every methods with an if-else statement, like

Code:
if($this->is_logged_in()
{
   //do something
}
else
{
   //redirect to home
}

Isn't it long and tedious? Is there an ultimate way to achieve this?

Thank you for helping me!