CodeIgniter Session

Previous Page

Session storage drivers in CodeIgniter:

  • files 
  • database
  • Redis
  • memcached

Initialization of a session:

Sessions run globally every time the page loads, the session class is initialized in the controller or can also be autoloaded by our system.the session class is run in the background all the time by default, we just have to create a class to make our system read it, we can also manipulate the session if needed.

Adding a session class manually inside the controller:

$this->load->library('session');

Calling a session:

$this->session

Working of session:

  • Every time the page loads, if the cookies sent by the user are valid or not.
  • If the cookies are invalid or expired, a new session will be generated and saved.
  • If cookies are matched and the session is valid, it starts updating all the information with each activity of a user, all this is done with the help of session ID.
  • Once a session is initialized it will keep running automatically.
  • All the work like reading, updating, and writing s sessions is done automatically.

Session Data:

  • Session data is stored in an array that is associated with its distinct session ID and cookies.
  • Unlike PHP sessions, Codeigniter also has similar working like reading, inserting, and updating data in arrays but it is more simplified.

Retrieving data from session:

$_SESSION is used to get the bit of code from the session array, globally.

Syntax:

$_SESSION['item']

Otherway using getter:

$this->session->item

Using userdata() method:

$this->session->userdata('item');

Using item name:

$name = $_SESSION['item-name'];
// else:
$name = $this->session->'item-name'
// else:
$name = $this->session->userdata('item-name');


We can also retrieve user data using $userdata():

$_SESSION
// else,
$this->session->userdata();

Adding data to the session:

  • When a client logs into our web application, we get the username, email, and other details we have requested in our query.
  • All of this data from the user is assigned to the $_SESSION which is an array.
  • Or we can use another method called set_userdata().
$this->session->set_userdata($array);

Example:

$newdata = array(

        'user_name'  => 'peter',
        'email_id'     => 'peter@gamil.com',
        'is_logged_in' => TRUE

);

$this->session->set_userdata($newdata);

We can also add one value at one point.

 $this->session->set_userdata('user_name', 'value'); 

We can check for a value in session using:

isset($_SESSION['user_name'])

Other way to call the data from the session using has_userdata() 

method:

$this->session->has_userdata('user_name');

Removing data from a session:

unset () method id used to remove any data from the session.

Example:

unset($_SESSION['user_name']);

// else  multiple values:

unset(

        $_SESSION['user_name'],

        $_SESSION[email_id]

);

Unlike setuserdata() method we can use unset_userdata() method also:

$this->session->unset_userdata('user_name');

Unset multiple values:

$array_items = array('user_name', 'email_id');

$this->session->unset_userdata($array_items);

Destroying a Session:

While the user logs out or clear history on their browser the session gets destroyed with the help of session_destroy() function or sess_destroy() function both of these functions work similarly.

 

Example:

session_destroy();
//OR
$this->session->sess_destroy();

Flashdata:

  • Flashdata is a form of session data.
  • This data is stored for the next server requests.
  • After the next request, the data gets cleared.
  • Flash data is usually used to display error/warning and one-time message/information to the user.

 

Marking existing session data item as “flashdata”:

$this->session->mark_as_flash('item');

Marking multiple items as a flashdata:

$this->session->mark_as_flash(array('item_1', 'item_2'));

Adding FlashData:

$_SESSION['item'] = 'value';

$this->session->mark_as_flash('item');

Alternate function to add flashdata using flash setdata() function:

$this->session->set_flashdata('item', 'value');

Reading items from flashdata:

$_SESSION['item']

OR//

$this->session->flashdata('item');

OR//

$this->session->flashdata();

Reading flashdata using keep_flashdata() function:

$this->session->keep_flashdata('item');

$this->session->keep_flashdata(array('item1', 'item2', 'item3'));

Tempdata:

  • Temp data is a form of session data with a specified expiration time with it.
  • The value is removed automatically if the specified time is expired or the session expires.
  • To mark the session data as a tempdata we use mark_as_temp().

 

Example:

$this->session->mark_as_temp('item', 500);

Marking multiple items a temp data in session:

$this->session->mark_as_temp(array(

        'item'  => 500,

        'item_2' => 1000

));

Adding Tempdata:

$_SESSION['item'] = 'value';

$this->session->mark_as_temp('item', 600); // Expire in 10 minutes

Using alternative method set_tempdata():

$this->session->set_tempdata('item', 'value', 600);

Using alternative method set_tempdata() with array:

$tempdata = array('new_user' => TRUE, 'message' => 'Hello from the session!');

$this->session->set_tempdata($tempdata, NULL, $expire);

Reading Tempdata:

$_SESSION['item']

Using tempdata() method:

$this->session->tempdata('item');

Retrieving all data from tempdata using tempdata():

$this->session->tempdata();

Removing tempdata before it expires:

unset($_SESSION['item']);

Removing tempdata using unset_tempdata() function before expiration:

$this->session->unset_tempdata('item');

 

Previous Page

Treding Tutorial

Popular Tutorials

to Learn now