CakePHP 会话管理

WBOY
发布: 2024-09-10 17:26:38
原创
800 人浏览过

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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板