Session handling in codeigniter


Published on

Session handling in codeigniter

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Session handling in codeigniter

  1. 1. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  2. 2. Session Handling in CodeIgniter SWALIH T www.facebook/ swalih321 9539353663
  3. 3. 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
  4. 4. 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. With each update, the session_id will be regenerated. • 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.
  5. 5. What is Session Data? • A session, as far as CodeIgniter is concerned, is simply an array containing the following information: • The user's unique Session ID (this is a statistically random string with very strong entropy, hashed with MD5 for portability, and regenerated (by default) every five minutes) • The user's IP Address • The user's User Agent data (the first 120 characters of the browser data string) • The "last activity" time stamp.
  6. 6. Retrieving Session Data • Any piece of information from the session array is available using the following function: • $this->session>userdata('item');Where item is the array index corresponding to the item you wish to fetch. For example, to fetch the session ID you will do this: • $session_id = $this->session>userdata('session_id');
  7. 7. Adding Custom Session Data • A useful aspect of the session array is that you can add your own data to it and it will be stored in the user's cookie. • this->session->set_userdata($array);Where $array is an associative array containing your new data. Here's an example: • $newdata = array( 'username' => 'johndoe', 'email' => '', 'logged_in' => TRUE ); $this->session->set_userdata($newdata);
  8. 8. Retrieving All Session Data • An array of all userdata can be retrieved as follows: • $this->session->all_userdata()And returns an associative array like the following: • Array ( [session_id] => 4a5a5dca22728fb0a84364eeb405b601 [ip_address] => [user_agent] => Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; [last_activity] => 1303142623 )
  9. 9. Removing Session Data Just as set_userdata() can be used to add information into a session, unset_userdata() can be used to remove it, by passing the session key. For example, if you wanted to remove 'some_name' from your session information: • $this->session->unset_userdata('some_name'); • This function can also be passed an associative array of items to unset. • $array_items = array('username' => '', 'email' => ''); $this->session->unset_userdata($array_items);
  10. 10. Destroy • To clear the current session: • $this->session->sess_destroy(); • if you only want some items destroyed and not all, use unset_userdata().
  11. 11. If this presentation helped you, please visit our page and like it. Thanks | |
  12. 12. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550