CakePHP Cookie 管理
使用 CakePHP 处理 Cookie 既简单又安全。有一个 CookieComponent 类用于管理 Cookie。该类提供了多种使用 Cookie 的方法。
要使用 cookie,请将这 2 个类添加到您的控制器-
use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieCollection;
必须首先创建 cookie 对象才能注册 cookie。
$cookie = new Cookie(name,value,expiration time,path,domain);
名称和值是必填项,其他为可选参数。
写入 Cookie
以下是编写 cookie 的语法。
$cookie = new Cookie(name,value,expiration time,path,domain);
创建的 cookie 必须添加到 cookieCollection 中,如下所示-
$cookie = new Cookie('name','XYZ');
$cookies = new CookieCollection([$cookie]);
如果已经创建了 cookie 集合对象,则可以添加其余的 cookie,如下所示-
$cookies = $cookies->add($cookie);
读取 Cookie
使用 cookiecollection 中的 get() 方法读取 cookie。
语法
系统读取 cookie 的 ntax 如下-
Cake\Http\Cookie\CookieCollection::get($name)
这将返回 cookiecollection 接口,要获取 cookie 的值,您必须调用 getValue() 方法。
Cake\Http\Cookie\CookieCollection Interface::getValue()
检查Cookie
cookieCollection 的
has() 方法会告诉你 cookie 是否存在。
Cake\Http\Cookie\CookieCollection::has($name)
示例
echo $isPresent = $this->cookies->has('name');
删除 Cookie
remove() 方法用于删除 cookie。以下是 remove() 方法的语法。
Cake\Http\Cookie\CookieCollection::remove($name)
remove() 方法将接受一个参数,即要删除的 cookie 变量 ($name) 的名称。
示例 1
$test = $this->cookies->remove('name');
示例 2
在 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('cookie/testcookies',['controller'=>'Cookies','action'=>'testCookies']);
$builder->fallbacks();
});
在
src/Controller/CookiesController.php 中创建一个 CookiesController.php 文件。 将以下代码复制到控制器文件中。
src/Controller/Cookies/CookiesController.php
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieCollection;
class CookiesController extends AppController{
public $cookies;
public function testCookies() {
$cookie = new Cookie('name','XYZ');
$this->cookies = new CookieCollection([$cookie]);
$cookie_val = $this->cookies->get('name');
$this->set('cookie_val',$cookie_val->getValue());
$isPresent = $this->cookies->has('name');
$this->set('isPresent',$isPresent);
$this->set('count', $this->cookies->count());
$test = $this->cookies->remove('name');
$this->set('count_afterdelete', $test->count());
}
}
?>
在
src/Template 中创建一个
Cookies 目录,然后在该目录下创建一个名为
test_cookies.php 的 View 文件。 b> 在该文件中复制以下代码。
src/Template/Cookie/test_cookies.php
The value of the cookie is: <?php echo $cookie_val; ?>
<br/>
<?php
if($isPresent):
?>
The cookie is present.
<?php
else:
?>
The cookie isn't present.
<?php
endif;
?>
<br/>
<?php
echo "The count of cookie before delete is :" .$count;
?>
<br/>
<?php
echo "The count of cookie after delete is :" .$count_afterdelete;
?>
输出
通过访问以下 URL 执行上述示例-http://localhost/cakephp4/cookie/testcookies
