目录

CakePHP - 会话管理( Session Management)

Session允许我们跨请求管理唯一用户并为特定用户存储数据。 会话数据可以在您有权访问请求对象的任何位置访问,即可以从控制器,视图,帮助程序,单元和组件访问会话。

访问会话对象

可以通过执行以下代码来创建会话对象。

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

编写会话数据

要在会话中编写内容,我们可以使用write() session方法。

Session::write($key, $value)

上面的方法将采用两个参数, value和将存储valuekey

例子 (Example)

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

阅读会话数据

要从会话中检索存储的数据,我们可以使用read() session方法。

Session::read($key)

上述函数将只接受一个参数, the key of the value参数是the key of the value写入会话数据时使用the key of the value 。 一旦提供了正确的密钥,该函数将返回其值。

例子 (Example)

$session->read('name');

如果要检查会话中是否存在特定数据,则可以使用check() session方法。

Session::check($key)

上面的函数只使用key作为参数。

例子 (Example)

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

删除会话数据

要从会话中删除数据,我们可以使用delete() session方法删除数据。

Session::delete($key)

上面的函数只接受从会话中删除的值的键。

例子 (Example)

$session->delete('name');

当您想要从会话中读取然后删除数据时,我们可以使用consume() session方法。

static Session::consume($key)

上面的函数只将key作为参数。

例子 (Example)

$session->consume('name'); 

销毁会话

当用户从站点注销并销毁会话时,我们需要销毁用户会话,使用destroy()方法。

Session::destroy()

例子 (Example)

$session->destroy();

销毁会话将从服务器中删除所有会话数据,但不会删除会话cookie。

续订会话

在您想要续订用户会话的情况下,我们可以使用renew() session方法。

Session::renew()

例子 (Example)

$session->renew();

完成课程

Make changes in the <b class="notranslate">config/routes.php</b> file as shown in the following program.

config/routes.php

<?php
   use Cake\Core\Plugin;
   use Cake\Routing\RouteBuilder;
   use Cake\Routing\Router;
   Router::defaultRouteClass('DashedRoute');
   Router::scope('/', function (RouteBuilder $routes) {
      $routes->connect('/sessionobject',
         ['controller'=>'Sessions','action'=>'index']);
      $routes->connect('/sessionread',
         ['controller'=>'Sessions','action'=>'retrieve_session_data']);
      $routes->connect('/sessionwrite',
         ['controller'=>'Sessions','action'=>'write_session_data']);
      $routes->connect('/sessioncheck',
         ['controller'=>'Sessions','action'=>'check_session_data']);
      $routes->connect('/sessiondelete',
         ['controller'=>'Sessions','action'=>'delete_session_data']);
      $routes->connect('/sessiondestroy',
         ['controller'=>'Sessions','action'=>'destroy_session_data']);
      $routes->fallbacks('DashedRoute');
   });
   Plugin::routes();

src/Controller/SessionsController.php创建一个SessionsController.php文件。 将以下代码复制到控制器文件中。

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->session();
         //read data from session
         $name = $session->read('name');
         $this->set('name',$name);
      }
      public function writeSessionData(){
         //create session object
         $session = $this->request->session();
         //write data in session
         $session->write('name','Virat Gandhi');
      }
      public function checkSessionData(){
         //create session object
         $session = $this->request->session();
         //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->session();
         //delete session data
         $session->delete('name');
      }
      public function destroySessionData(){
         //create session object
         $session = $this->request->session();
         //destroy session
         $session->destroy();
      }
   }
?>

src/Template和该目录下创建一个目录Sessions ,创建一个名为write_session_data.ctpView文件。 复制该文件中的以下代码。

src/Template/Sessions/write_session_data.ctp

The data has been written in session.

在同一个Sessions目录下创建另一个名为retrieve_session_data.ctp View文件,并将以下代码复制到该文件中。

src/Template/Sessions/retrieve_session_data.ctp

Here is the data from session.
Name: <?=$name;?>

在同一个Sessions目录下创建另一个名为check_session_data.ctp View文件,并将以下代码复制到该文件中。

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

在同一个Sessions目录下创建另一个名为delete_session_data.ctp filbe View并在该文件中复制以下代码。

src/Template/Sessions/delete_session_data.ctp

Data deleted from session.

在同一个Sessions目录下创建另一个名为destroy_session_data.ctp View文件,并在该文件中复制以下代码。

src/Template/Sessions/destroy_session_data.ctp

Session Destroyed.

输出 (Output)

通过访问以下URL执行上述示例。 此URL将帮助您在会话中写入数据。

http://localhost:85/CakePHP/session-write

会议

访问以下URL to read session data - http://localhost:85/CakePHP/session-read

CakePHP会话

访问以下URL to check session data - http://localhost:85/CakePHP/sessioncheck

会议

访问以下URL to delete session data - http://localhost:85/CakePHP/sessiondelete

删除会话

访问以下URL to destroy session data - http://localhost:85/CakePHP/sessiondestroy

会话被破坏
↑回到顶部↑
WIKI教程 @2018