11-2 ThinkPHP5 cookie的使用

11-2 ThinkPHP5 cookie的使用

天答翔踌布铅挫烫谁勉莎水衔

cookie的配置可以复制惯例配置文件中的内容到config.php

11-2 ThinkPHP5 cookie的使用第1张

<?php
return [
    'cookie'        => [
        // cookie 名称前缀
        'prefix'    => '',
        // cookie 保存时间
        'expire'    => 0,
        // cookie 保存路径
        'path'      => '/',
        // cookie 有效域名
        'domain'    => '',
        //  cookie 启用安全传输
        'secure'    => false,
        // httponly设置
        'httponly'  => '',
        // 是否使用 setcookie
        'setcookie' => true,
    ],
];

11-2 ThinkPHP5 cookie的使用第2张

1.设置cookie,有效期3600秒

<?php
namespace app\index\controller;
use think\Controller;
use think\Cookie;
class Index extends Controller
{
    public function index()
    {
        Cookie::set('user_name','mengmianren',3600);
        dump($_COOKIE);
    }

}

执行:

11-2 ThinkPHP5 cookie的使用第3张


2.设置前缀为think

<?php
namespace app\index\controller;
use think\Controller;
use think\Cookie;
class Index extends Controller
{
    public function index()
    {
        Cookie::set('user_name','mengmianren',['prefix'=>'think_','expire'=>3600]);
        dump($_COOKIE);
    }

}

执行:

11-2 ThinkPHP5 cookie的使用第4张


3.支持设置数组

<?php
namespace app\index\controller;
use think\Controller;
use think\Cookie;
class Index extends Controller
{
    public function index()
    {
        Cookie::set('Teacher',['赵老师','钱老师','孙老师','李老师']);
        dump($_COOKIE);
    }

}

11-2 ThinkPHP5 cookie的使用第5张

设置数组值为英文

<?php
namespace app\index\controller;
use think\Controller;
use think\Cookie;
class Index extends Controller
{
    public function index()
    {
        Cookie::set('Teacher',['zhao','qian','sun','li']);
        dump($_COOKIE);
    }

}

执行:

11-2 ThinkPHP5 cookie的使用第6张


4.读取cookie

<?php
namespace app\index\controller;
use think\Controller;
use think\Cookie;
class Index extends Controller
{
    public function index()
    {
        echo Cookie::get('user_name');
    }

}

执行:

11-2 ThinkPHP5 cookie的使用第7张


5.获取指定前缀的值:think_user_name

<?php
namespace app\index\controller;
use think\Controller;
use think\Cookie;
class Index extends Controller
{
    public function index()
    {
        echo Cookie::get('user_name','think_');
    }

}

执行:

11-2 ThinkPHP5 cookie的使用第7张


6.建议通过request请求对象来读取coookie和session

<?php
namespace app\index\controller;
use think\Controller;
use think\Cookie;
class Index extends Controller
{
    public function index()
    {
        //使用请求对象来读取cookie(也可以读取session)
        echo $this->request->cookie('user_name');
    }

}

执行:

11-2 ThinkPHP5 cookie的使用第9张


7.判断cookie是否存在

<?php
namespace app\index\controller;
use think\Controller;
use think\Cookie;
class Index extends Controller
{
    public function index()
    {
        //使用请求对象来读取cookie(也可以读取session)
        echo Cookie::has('user_name');
    }

}

执行:

11-2 ThinkPHP5 cookie的使用第10张

判断含有前缀的值是否存在

<?php
namespace app\index\controller;
use think\Controller;
use think\Cookie;
class Index extends Controller
{
    public function index()
    {
        //使用请求对象来读取cookie(也可以读取session)
        echo Cookie::has('user_name','think_');
    }

}

执行:

11-2 ThinkPHP5 cookie的使用第11张


8.删除user_name

<?php
namespace app\index\controller;
use think\Controller;
use think\Cookie;
class Index extends Controller
{
    public function index()
    {
        Cookie::delete('user_name');
        dump($_COOKIE);
    }

}

执行:

11-2 ThinkPHP5 cookie的使用第12张


9.删除指定前缀的cookie

<?php
namespace app\index\controller;
use think\Controller;
use think\Cookie;
class Index extends Controller
{
    public function index()
    {
        Cookie::delete('user_name','think_');
        dump($_COOKIE);
    }

}

执行:

11-2 ThinkPHP5 cookie的使用第13张


10.清空指定前缀的cookie

先设置cookie

<?php
namespace app\index\controller;
use think\Controller;
use think\Cookie;
class Index extends Controller
{
    public function index()
    {
        Cookie::set('siteName','萌面人资料铺');
        Cookie::set('siteName','萌面人资料铺',['prefix'=>'think_']);
        dump($_COOKIE);
    }

}

执行:

11-2 ThinkPHP5 cookie的使用第14张

清空前缀为think_的cookie

<?php
namespace app\index\controller;
use think\Controller;
use think\Cookie;
class Index extends Controller
{
    public function index()
    {
        Cookie::clear('think_');
        dump($_COOKIE);
    }

}

执行:

11-2 ThinkPHP5 cookie的使用第15张


助手函数的使用可以查看手册


模板中使用cookie

11-2 ThinkPHP5 cookie的使用第16张

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:

<?php
namespace app\index\controller;
use think\Controller;
use think\Cookie;
class Index extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

}

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:

<p>网站名:{$Request.cookie.siteName}</p>

执行:

11-2 ThinkPHP5 cookie的使用第17张


模板还可以这样写:

<p>网站名:{$_COOKIE['siteName']}</p>

执行:

11-2 ThinkPHP5 cookie的使用第17张


低橙释须迫缆穆枯桑苦氏解动