CodeIgniter Forums

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

El Forum

[eluser]mistress_shiira[/eluser]
good day guys!
i was not using any framework before and do all the coding "hard-core" as you say. Smile
well,usually when i do have sessions,i usually include it at the top of the page and declare it like so:
Quote:
session_start();
if($_SESSION['logged_in'] != TRUE){
header("Location: login.php");
exit();
}
now,i dont know how i am going to achieve it using codeigniter now.i have already created a ci_sessions table containing the following fields: user_id,ip_address,user_agent,last_activity and logged_in.
any links to resources i can read and code examples are greatly appreciated.i learn a lot just by looking at codes and how it was implemented.Smile
thanks a lot!

El Forum

[eluser]llbbl[/eluser]
Session class

Quote:Initializing a Session

Sessions will typically run globally with each page load, so the session class must either be initialized in your controller constructors, or it can be auto-loaded by the system. For the most part the session class will run unattended in the background, so simply initializing the class will cause it to read, create, and update sessions.

To initialize the Session class manually in your controller constructor, use the $this->load->library function:
$this->load->library('session');

Once loaded, the Sessions library object will be available using: $this->session
How do Sessions work?

When a page is loaded, the session class will check to see if valid session data exists in the user's session cookie. If sessions data does not exist (or if it has expired) a new session will be created and saved in the cookie. If a session does exist, its information will be updated and the cookie will be updated.

It's important for you to understand that once initialized, the Session class runs automatically. There is nothing you need to do to cause the above behavior to happen. You can, as you'll see below, work with session data or even add your own data to a user's session, but the process of reading, writing, and updating a session is automatic.

If you autoload session class in your config file it will start a new session everytime it needs to automatically. This is a global change to your entire site.