CakePHP - Cookie 管理



使用 CakePHP 处理 Cookie 既简单又安全。有一个 CookieComponent 类用于管理 Cookie。该类提供了几种用于处理 Cookie 的方法。

要使用 Cookie,请将这两个类添加到您的控制器中:

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]);

如果 cookieCollection 对象已创建,则可以按如下所示添加其余的 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 文件。

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

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 执行上述示例:https://127.0.0.1/cakephp4/cookie/testcookies

Cookie
广告