CakePHP-Cookie管理



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

要使用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 = 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文件中进行更改。

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

曲奇饼

Made with ❤️ in Chengdu. Copyright reserved 2019-2022.

蜀ICP备20006366号-1