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

要读取 cookie,请使用 cookiecollection 中的 get() 方法。

句法

读取 cookie 的语法如下 -

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 文件中进行更改,如以下程序所示。

配置/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 的视图文件将以下代码复制到该文件中。

src/模板/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

曲奇饼