I am using Code Igniter and I am implementing a remember user functionality.
Basically, from another stack overflow post, I implemented this the right way where I generate a random string for that user, save it in the database AND in a cookie. On site load, I check for that cookie, if that cookie is found I check it in the database. If it is found in the database then recrease the session for that user.
The problem I am having is when I load the site. I am getting the value of the cookie and I am also getting the correct response from the AJAX call. However, I have to click on a link in order for the session to get recreated (such as menu is displayed for logged in user and so on).
I am thinking that I am recreating the session AFTER the index method of the main controller is called. How can I get around this in Code Igniter? Where can I put this code which preferably gets run first thing on every page? As for instance I also want to recreate the session if the user enters the site’s contact us page instead of the home page.
Many thanks in advance.
CodeIgniter allows the developer to create hooks which are called at different moments before the controller method is called.
Here is a short description of how to use hooks:
http://codeigniter.com/user_guide/general/hooks.html
If you want to access the session, I would recommend to use a
post_controller_constructorhook, which is called after the controller constructor is executed, but before the action method is called. ( access the CI session in a pre controller codeigniter hook )