Sessions

Sessions allow us to get access to data across various pages for each user request. A file is created in a temporary directory on the server by session to store all session variables. This stored data is available to all the pages of a web site during the visit of a particular user.

In PHP, session is accessed through the $_SESSION global variable.

When a session starts,

  • A unique ID is generated for that particular session.
  • A cookie is sent to the client side.
  • All these session variables are saved in the temporary folder on the server.
  • When a value is retrived from the session variable, it automatically gets unique session ID from the cookie. Then it looks into its temporary folder for the particular file.

Sessions Opening and Closing

Let's see an example of opening and closing a session.

Step 1 Go to the SiteController.php file. Add the action actionCheckStatus.

Look at the above code, it shows session opening, session closing, checks whether session is active or not and destroys the session.

Step 2 Run it on the browser with the URL,

http://localhost/sess/frontend/web/index.php?r=site/check-status

YII Session 1

Accessing Session Data

During accessing of the data, if no session is running than session will automatically start it.

To access the data stored in the session, run the following code.

Next TopicYII Flash Data




Latest Courses