Session Library

The session is an essential part of any application. It contains various functions to manage the users’ status and track their activity while browsing on the site.

For example, when a user logs into any web application, it creates a global session for that user, tracking their activity and status until the user logs out of an application.

Load a Session Library

Before using the Session library in the Codeigniter application, you must load this library in the controller file.

Syntax

When the session library is loaded, you can simply use the session object to execute their function:

Class Reference

  1. A userdata() function is used to fetch only a specified data item form the session.

Syntax

$key: It defines the session items key or null.

  • As the name defines, an all_userdata() function is used to fetch all “userdata” items stored in the session.

Syntax

  • It is used to check whether the defined item exists in $_SESSION or not.

Syntax

$key: It defines the session key item.

  • As the name suggests, a set_userdata() function is used to set the data items to the $_SESSION  superglobal.

Syntax

It has two parameters

$data: You can define an array of key/value pairs or a single key item to set as session data.

$value: In this field, you can set the value for a specific session item.

  • An unset_userdata() function is used to remove the specified key value or array of multiple keys from the stored $_SESSION value.

Syntax

$key: It defines the key or array of multiple key to unset data item.

  • A sess_destroy() function is used to destroy the current session.

Syntax

  • A sess_regenerate() function is used to regenerate the session id. It also provides an optional parameter to determine whether you want to destroy the current session’s data.

Syntax

$destroy: It uses a Boolean value to destroy session data.

  • It is a magic method that enable you to fetch data items by using $this->session->data instead of $_SESSION[‘data’].  
  • It is also a magic method that enables you to set data items to $_SESSION by accessing them as $this->session properties:

Syntax

                     It has two parameters:

                     $key: It defines the session item key.

                     $value:  It defines the value which you want to assign to the session item key.

Functions of Session Library

  1. Add session data

In this topic, we will understand how we can add data to the session class. For example, when you log into a site, it authenticates you by taking ‘username’ and ‘email’ address as an input to create a session on the site.

Case1. Add multiple items in an associative array.

Syntax

Case2. When you want to add one value at a time in a set_userdata() function.

Syntax

Create a Session_controller.php controller file and save it to the following path application/controller/Session_controller.php folder. After that, write the following program in the controller file.

Session_controller.php

Create a session_view.php view file and save it to the following path application/views/session_view.php folder. After that, write the following statement to show a message.

Session_view.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Session_controller/set_session function; it shows the output, as shown below.

Session Library
  • Read SESSION DATA

Now in this topic, we will understand how we can retrieve a piece of information from the session that is available through the $_SESSION. It contains various methods to fetch particular data or an array of data items from the session.

Case1. Retrieve a single item to the session such as;

Or through the magic getter:

Case2. Retrieve a single item or an array of data to the session.

Example: Create a Session_controller.php controller file and save it to the following path application/controller/Session_controller.php folder. After that, write the following program in the controller file.

Session_controller.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Session_controller/readSession function; it shows the output, as shown below.

Session Library
  • Check an Items from Session

In this method, you can check the session value is exists or not, by using the following syntax, as shown below:

  1. isset($_SESSION[‘data_name’]);

If the defined item is not existing in the session, it returns FALSE or NULL; otherwise, it shows TRUE.

  • Or you can all has_userdata() function to check the data item in the session.

Create a Session_controller.php controller file and save it to the following path application/controller/Session_controller.php folder. After that, write the following program in the controller file.

Session_controller.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Session_controller/check function; it shows the output, as shown below.

Session Library
  • Remove Session Data

In this topic, we will understand how we can remove or unset a value in $_SESSION with the help of unset() function. An unset() function can reset more than one data item in $_SESSION.

There are following method available in remove session to unset the data:

  1. unset single item
  • unset multiple data: You can define multiple data items in $_SESSION to reset the stored data value in the session class.
  • unset_userdata(): An unset_userdata() function is also used to remove a specific item or an array of data from the stored session.

For example:

Create a Session_controller.php controller file and save it to the following path application/controller/Session_controller.php folder. After that, write the following program in the controller file.

Session_controller.php

Create a remove_view.php view file and save it to the following path application/Views/remove_view.php. After that, write the following statement to show a message.

remove_view.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Session_controller/remove function; it shows the output, as shown below.

Session Library
  • Destroy a Session

If you want to clear the current session in any site (for example, during logout), you can directly use the below syntax as shown below:

Syntax

Create a Session_controller.php controller file and save it to the following path application/controller/Session_controller.php folder. After that, write the following program in the controller file.

Session_controller.php

Create a destroy_view.php view file and save it to the following path application/Views/destroy_view.php. After that, write the following statement to show a message.

destroy_view.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Session_controller/destroy function; it shows the output, as shown below.

Session Library
  • Flashdata

A Flashdata is an important part of a web application that displays flash messages on a web site. In Flashdata, we need to store some data that is only available to display an error message or notification message once. When a new request for the application arrives, it is automatically deleted.

  1. Add Flash message

There are two ways to add flashdata to the session.

  1. mark_as_flash(): It is used to add flash messages or to mark an existing item as flashdata. To store a single value, it requires only one argument as ‘item_name’, and if you want to store multiple values, you can use an array.

Syntax

  • set_flashdata(): As the name suggests, a set_flashdata() function is also used to set flash messages. It takes two argument ‘name’ and ‘value’ of the flashdata to store a single item, as shown below. You can also pass an array to set_flashdata() function.

Syntax

  • Retrieve Flashdata

If you want to fetch or display a particular message to the application, you can use a flashdata() function that takes one argument to be fetched from a flash session, as shown below.

Syntax

NOTE: If you have not passed any argument to the flashdata() function, it returns an array with the same function.

Example: Create a Session_controller.php controller file and save it to the following path application/controller/Session_controller.php folder. After that, write the following program in the controller file.

Session_controller.php

Create a flashdata_home.php view file and save it to the following path application/Views/flashdata_home.php. After that, write the following program in the view file.

flashdata_home.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Session_controller, it shows the output, as shown below.

Session Library

As you click on the ‘Click Here’ link, it displays a given below image. In this image, you can see there are two flash messages on the screen. When you refresh this page, it automatically removes the flash messages from your screen, and it will show you as an above screen.

Session Library
  • Tempdata

A Codeigniter provides a tempdata functionality used to remove the value of an item with a specific expiration time that is stored in a session. When the expiration time expires, the value is automatically removed or removed from the session.

There are the following functions available in the tempdata:

Add message using Tempdata

  1. mark_as_temp(): It is used to add temp message or to mark an existing item as tempdata with its key and expiry time (in second). It takes two arguments ‘item_name’ and the expiration ‘time’ for the values to be stored in session tempdata. You can also define an array to store multiple values in the session.

Syntax

  • set_tempdata(): As the name suggests, a set_tempdata() function is also used to set temp messages with a defined expiration time (in seconds). After that, it automatically removes from the session. It takes three arguments, such as:
  • The First parameter takes the key as an ‘item_name’.
  • The second parameter requires the ‘value’ of a defined key name.
  • And the last parameter takes the expiration time (in second).

Example

     Read Tempdata Message

If you want to read a template message from the session, you can use the tempdata() function. It allows you to read a single item as well as multiple existing tempdata from the session.

Example:

Read single item:

Read multiple data:

     Removes Tempdata

A tempdata can be removed automatically from the session after the completion of the expiration time. If you want to remove temp messages before its expiration time, you can use unset_tempdata() function., which takes only one argument of the item to be removed. Furthermore, if you want to remove multiple values, you can leave unset_tempdata() function to blank.

Example:

Create a Session_controller.php controller file and save it to the following path application/controller/Session_controller.php folder. After that, write the following program in the controller file.

Session_controller.php

Create a tempdata_home.php view file and save it to the following path application/Views/tempdata_home.php. After that, write the following program in the view file.

tempdata_home.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Session_controller/index_home, it shows the output, as shown below.

Session Library

As you click on the ‘Click Here’ link, it displays a given below image. In this image, there are three temp messages which are available for 10 and 20 seconds.

Session Library

After refreshing the web page in 10 seconds, you will find a below image. In this image, you can see, a third temp message – ‘Welcome To The Tutorial and Example’ has been disappeared due to the completion of the defined expiration time. Similarly, when the 20 seconds have been passed, the remaining messages will also be removed, and it redirects you to the main page of the tempdata.

Session Library

Pin It on Pinterest

Share This