首页 >后端开发 >php教程 >CakePHP 会话管理

CakePHP 会话管理

WBOY
WBOY原创
2024-09-10 17:26:38807浏览

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();

Writing Session Data

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

Session::write($key, $value)

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');

Reading Session Data

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

Session::read($key)

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');

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)

The above function will take only key as the argument.

Example

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

Delete Session Data

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

Session::delete($key)

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

Example

$session->delete('name');

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

static Session::consume($key)

The above function will take only key as argument.

Example

$session->consume('name');

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()

Example

$session->destroy();

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()

Example

$session->renew();

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();
});

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();
   }
}
?>

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.

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 会话管理: =$name;?>

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;?>

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.

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 会话管理.

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 会话管理

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

CakePHP 会话管理 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 会话管理

以上是CakePHP 会话管理的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn