CakePHP 会话管理
Session 允许我们跨请求管理唯一用户,并为特定用户存储数据。会话数据可以在您有权访问请求对象的任何地方、任何地方访问,即可以从控制器、视图、助手、单元格和组件访问会话。
访问会话对象
可以通过执行以下代码来创建会话对象。
$session = $this->request->session();
写入会话数据
要在 session 中写东西,我们可以使用
write() session 方法。
Session::write($key, $value)
上述方法将采用两个参数,
value 和
key 下的值将被存储。
示例
$session->write('name', 'Virat Gandhi');
读取会话数据
要从 session 中检索存储的数据,我们可以使用
read() session 方法。
上述函数将只接受一个参数,即
值的键,在写入会话数据时使用。一旦提供了正确的密钥,函数就会返回它的值。
示例
当您想检查会话中是否存在特定数据时,您可以使用
check() session 方法。
上述函数将只接受 key 作为参数。
示例
if ($session->check('name')) {
// name exists and is not null.
}
删除会话数据
要从 session 中删除数据,我们可以使用
delete() session 方法删除数据。
上述函数将只获取要从会话中删除的值的键。
示例
$session->delete('name');
当你想从 session 中读取然后删除数据时,我们可以使用
consume() session 方法。
static Session::consume($key)
上述函数将只接受键作为参数。
示例
$session->consume('name');
销毁会话
我们需要销毁一个用户会话,当用户从站点注销并销毁会话时,使用
destroy() 方法。
示例
销毁会话将从服务器中删除所有会话数据,但不会删除会话 cookie。
续订会话
如果您想续订用户会话,我们可以使用
renew() session 方法。
示例
完成会话
在
config/routes.php 文件中进行更改,如以下程序所示。
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();
});
在
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->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();
}
}
?>
在
src/Template 创建一个目录
Sessions,然后在该目录下创建一个名为
write_session_data.php 的 View 文件。 b> 在该文件中复制以下代码。
src/Template/Sessions/write_session_data.php
The data has been written in session.
在同一
Sessions 目录下创建另一个名为
retrieve_session_data.php 的
View 文件,并将以下代码复制到该文件中。
src/Template/Sessions/retrieve_session_data.php
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, 的
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
输出
通过访问以下 URL 执行上述示例。此 URL 将帮助您在会话中写入数据。
http://localhost/cakephp4/session-write
访问以下 URL
读取会话数据-http://localhost/cakephp4/session-read
访问以下 URL
检查会话数据-http://localhost/cakephp4/session-check
访问以下 URL
删除会话数据-http://localhost/cakephp4/session-delete 访问
访问以下 URL
销毁会话数据-http://localhost/cakephp4/session-destroy
