Session in CakePHP

A Session allows you to recognize as a unique user across all the requests generated on the server, and it also stores data for specific users. Unlike cookies, session data is also not available on the clients’ side. We can also access session data anywhere when you got an object requested on the server. Each session has a unique id that is used to fetch the stored data from the session. A Session can also be accessible by controller, views, helpers, cells, and components.

Various Operation of Session

1) Start a Session

We can start the session by executing the following code.

 $this->request->session();

And if you want to access the session in all operation like as write, read, destroy and delete then you have to use session as a local object like:

$session = $this->request->session();

Or by this

$session = $this->getRequest()->getSession();

2) Write a Session

If you want to write data in your session, then you can use write() session method

Session::write ($key, $value)

The above method will take two parameters, the key and the value in which the value will be stored.

Example

$session->write ('name', ‘Amitabh’);

Or you can also use this method to show more than one parameters in write method like this-

 $session->write ([
 'Config.theme'=>'blue',
 'Config.language'=>'English',
 ]); 

Here we pass two parameters in an array as key-value pairs.  

3) Read a Session data

You can fetch stored data from the session by using the read() session method.

Session::read ($key)

The above function will take one parameter; that’s the key that was used at that time of writing session data. When you pass the correct key in an argument, then it will return the value of a key.

Example

$session->read(' name ');

4) Check session

In this function, you can check whether your data is presented in session or not by use of the check() method.

Session::check($key)

In the above function, you can pass a key as the argument to check the session.

Example

  ($session->check('name')) {
  // name exists or not.
 } 

5) Delete a Session

If you want to delete session data, you can use the delete() session method.

Session::delete($key)

You have to pass a key in delete() method to delete the key’s value from the session.

Example

$session->delete(' name ');

6) Consume a Session

When you want to read and delete data from the session, then you have to use the consume() session method.

static Session::consume($key)

This function will take the only key as an argument.

Example

$session->consume('name');

7) Destroying a Session

In this method, we can use a destroy () function to destroy the session when the user logs out from the site.

Session::destroy()

Example

$session->destroy();

When we use a destroy session, then it removes all session data from the server, but it will not remove the cookies.

8) Renew a Session

In some situations user wants to renew the session then it can be accessed by renew() session method.

Session::renew()

Example

$session->renew();

Here we create a session example in which we have performed all operations of the session.

Create an FSessionsController.php file at src/Controller/FSessionsController.php. Now write the following code in the controller file.

src/Controller/FSessionsController.php

 request->session();
 //readdatafromsession
  $name=$session->read('name');
  $theme=$session->read('Config.theme');
  $language=$session->read('Config.language');
 $this->set('name', $name);
 $this->set('theme', $theme);
 $this->set('language', $language);
 }
 publicfunctionwritedata(){
 //createsessionobject
 //$session=$this->request->session();
 $session=$this->getRequest()->getSession();
  //writedatainsession
 $session->write('name', 'Amitabh' );
 $session->write( [
 'Config.theme'=>'blue',
 'Config.language'=>'English',
 ] );
 }
 publicfunctioncheckdata(){
 //createsessionobject
 $session=$this->request->session();
 //checksessiondata
 $name=$session->check('name');
 $theme=$session->check('Config.theme');
 $language=$session->check('Config.language');
 $this->set('name', $name);
 $this->set('theme', $theme);
 $this->set('language', $language);
 }
 publicfunctiondeletedata(){
 //createsessionobject
 $session=$this->request->session();
 //deletesessiondata
 $session->delete('name');
 $session->delete('Config.theme');
 }
 publicfunctiondestroydata(){
 //createsessionobject
 $session=$this->request->session();
 //destroysession
 $session->destroy();
 }
 }
 ?> 

Create a folder FSessions at src/Template, and that folder create a write_data.ctp file. Here we have written some text in the write_data.ctp file.

Your info has been written in session.

Now we will run our program in localhost like localhost/CakePHP3.8/FSession/write_data

Output

Session in CakePHP

Create a folder FSessions at src/Template, and that folder create a read_data.ctp file. Here we have written some code to read the file.

Here is info from session.

 ";?>
 Name:
 ";?>
 colour:
 ";?>
 Language: 

Now we will run our program in localhost like localhost/CakePHP3.8/FSession/read_data

Output

Session in CakePHP 1

Create a folder FSessions at src/Template, and that folder create a check_data.ctp file. Here we have written some code to check the session in our file

 
 yourinformationhasbeensuccessfullysaveinsession.
 
 informationisnotstoredindatabase 
 
 
 languageexistsinthesession.
 
 languagedoesn'texistinthedatabase
  

Now we will run our program in localhost like localhost/CakePHP3.8/FSession/check_data

Output

Session in CakePHP 2

Create a folder FSessions at src/Template, and that folder create a delete_data.ctp file. Here we have written some text in our session file.

Information deleted from session.

Now we will run our program in localhost like localhost/CakePHP3.8/FSession/delete_data

Output

Session in CakePHP 3

Create a folder FSessions at src/Template, and that folder create a destroy_data.ctp file. Here we have written some text in our session file.

Session is Destroyed.

Now we will run our program in localhost like localhost/CakePHP3.8/FSession/destroy_data

Output

Session in CakePHP 4

Session timeout

Session.timeout() represents that when the user does not perform any action on the site during an interval. Then it terminates the site session; it means your session has been expired from the web service.

For read session time

 $timeout = $session->read ('session. timeout');
 $this->set('timeout', $timeout); 

For write session timeout

 $session->write( [
 'Session.timeout'=>02 // for 2 minutes
 ]);