What are sessions in PHP . How to read, write and delete session in cakephp?


Posted On: Apr 26, 2024


Session in PHP

PHP Sessions allows you to identify unique users across requests and store persistent data for specific users against a unique session ID. This can be used to persist state information between page requests. Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data.

Reading, Writing and Deleting session in Cakephp 3.x

You can access the session data any place you have access to a request object. This means the session is accessible from:

  • Controllers
  • Views
  • Helpers
  • Cells
  • Components

In addition to the basic session object, you can also use the Cake\View\Helper\SessionHelper to interact with the session in your views. A basic example of session usage would be:

$name = $this->request->session()->read('User.name');
// If you are accessing the session multiple times,
// you will probably want a local variable.
$session = $this->request->session();
$name = $session->read('User.name');

Reading, Writing and Deleting Session Data

Session::read($key) function is used to read specific session data in CakePHP.

Session::write($key, $value) function is used to write session data in CakePHP.

Session::delete($key) function is used to delete specific session data in CakePHP.

Sample Usage

//Reading a session
//Writing a session
$session->write('Config.language', 'en');
//Deleting a session

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Cakephp Interview Questions

    What is CakePHP ?

    CakePHP is an open-source free web framework written in PHP scripting Language for rapid web development...

    Cakephp Interview Questions

    What is MVC in CakePHP?

    MVC stands for the model view controller.MVC is not a design pattern, it is an architectural pattern that describes a way to structure our application and explains the responsibilities and interaction..