CakePHP Session Management

WBOY
Release: 2024-09-10 17:26:38
Original
683 people have browsed it

Session allows us to manage unique users across requests, and stores data for specific users. Session data can be accessible anywhere, anyplace, where you have access to request object, i.e., sessions are accessible from controllers, views, helpers, cells, and components.

Accessing Session Object

Session object can be created by executing the following code.

$session = $this->request->session();
Copy after login

Writing Session Data

To write something in session, we can use the write() session method.

Session::write($key, $value)
Copy after login

The above method will take two arguments, the value and the key under, which the value will be stored.

Example

$session->write('name', 'Virat Gandhi');
Copy after login

Reading Session Data

To retrieve stored data from session, we can use the read() session method.

Session::read($key)
Copy after login

The above function will take only one argument, that is the key of the value, which was used at the time of writing session data. Once the correct key was provided, then the function will return its value.

Example

$session->read('name');
Copy after login

When you want to check whether, particular data exists in the session or not, then you can use the check() session method.

Session::check($key)
Copy after login

The above function will take only key as the argument.

Example

if ($session->check('name')) {
   // name exists and is not null.
}
Copy after login

Delete Session Data

To delete data from session, we can use the delete() session method to delete the data.

Session::delete($key)
Copy after login

The above function will take only key of the value to be deleted from session.

Example

$session->delete('name');
Copy after login

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

static Session::consume($key)
Copy after login

The above function will take only key as argument.

Example

$session->consume('name');
Copy after login

Destroying a Session

We need to destroy a user session, when the user logs out from the site and to destroy the session the destroy() method is used.

Session::destroy()
Copy after login

Example

$session->destroy();
Copy after login

Destroying session will remove all session data from server, but will not remove session cookie.

Renew a Session

In a situation, where you want to renew user session then, we can use the renew() session method.

Session::renew()
Copy after login

Example

$session->renew();
Copy after login

Complete Session

Make changes in the config/routes.php file as shown in the following program.

config/routes.php

<?php use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
   $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
      'httpOnly' => true,
   ]));
   $builder->applyMiddleware('csrf');
   //$builder->connect('/pages',['controller'=>'Pages','action'=>'display', 'home']);
   $builder->connect('/session-object',['controller'=>'Sessions','action'=>'index']);
   $builder->connect('/session-read',['controller'=>'Sessions','action'=>'retrieve_session_data']);
   $builder->connect('/session-write',['controller'=>'Sessions','action'=> 'write_session_data']);
   $builder->connect('/session-check',['controller'=>'Sessions','action'=>'check_session_data']);
   $builder->connect('/session-delete',['controller'=>'Sessions','action'=>'delete_session_data']);
   $builder->connect('/session-destroy',['controller'=>'Sessions','action'=>'destroy_session_data']);
   $builder->fallbacks();
});
Copy after login

Create a SessionsController.php file at src/Controller/SessionsController.php. Copy the following code in the controller file

src/Controller/SessionsController.php

<?php namespace App\Controller;
use App\Controller\AppController;
   class SessionsController extends AppController {
   public function retrieveSessionData() {
      //create session object
      $session = $this->request->getSession();
      //read data from session
      $name = $session->read('name');
      $this->set('name',$name);
   }
   public function writeSessionData(){
      //create session object
      $session = $this->request->getSession();
      //write data in session
      $session->write('name','Virat Gandhi');
   }
   public function checkSessionData(){
      //create session object
      $session = $this->request->getSession();
      //check session data
      $name = $session->check('name');
      $address = $session->check('address');
      $this->set('name',$name);
      $this->set('address',$address);
   }
   public function deleteSessionData(){
      //create session object
      $session = $this->request->getSession();
      //delete session data
      $session->delete('name');
   }
   public function destroySessionData(){
      //create session object
      $session = $this->request->getSession();
      //destroy session
      $session->destroy();
   }
}
?>
Copy after login

Create a directory Sessions at src/Template and under that directory create a View file called write_session_data.php. Copy the following code in that file.

src/Template/Sessions/write_session_data.php

The data has been written in session.
Copy after login

Create another View file called retrieve_session_data.php under the same Sessions directory and copy the following code in that file.

src/Template/Sessions/retrieve_session_data.php

Here is the data from session.
CakePHP Session Management: =$name;?>
Copy after login

Create another View file called check_session_data.ctp under the same Sessions directory and copy the following code in that file.

src/Template/Sessions/check_session_data.ctp

<?php if($name): ?>
name exists in the session.
<?php else: ?>
name doesn't exist in the database
<?php endif;?>
<?php if($address): ?>
address exists in the session.
<?php else: ?>
address doesn't exist in the database
<?php endif;?>
Copy after login

Create another View file called delete_session_data.ctp, under the same Sessions directory and copy the following code in that file.

src/Template/Sessions/delete_session_data.ctp

Data deleted from session.
Copy after login

Create another View file called destroy_session_data.ctp, under the same Sessions directory and copy the following code in that file.

src/Template/Sessions/destroy_session_data.ctp

Session CakePHP Session Management.
Copy after login

Output

Execute the above example by visiting the following URL. This URL will help you write data in session.

http://localhost/cakephp4/session-write

Written in Session

Visit the following URL to read session data − http://localhost/cakephp4/session-read

CakePHP Session Management

Visit the following URL to check session data − http://localhost/cakephp4/session-check

CakePHP Session Management Exists

Visit the following URL to delete session data − http://localhost/cakephp4/session-delete Visit the

Delete From Session

Visit the following URL to destroy session data − http://localhost/cakephp4/session-destroy

CakePHP Session Management

The above is the detailed content of CakePHP Session Management. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!