卧森韩速唬卯洛烷谁恒沛淋沪
cookie的配置可以复制惯例配置文件中的内容到config.php
<?php return [ 'cookie' => [ // cookie 名称前缀 'prefix' => '', // cookie 保存时间 'expire' => 0, // cookie 保存路径 'path' => '/', // cookie 有效域名 'domain' => '', // cookie 启用安全传输 'secure' => false, // httponly设置 'httponly' => '', // 是否使用 setcookie 'setcookie' => true, ], ];
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); } }
执行:
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); } }
执行:
3.支持设置数组
<?php namespace app\index\controller; use think\Controller; use think\Cookie; class Index extends Controller { public function index() { Cookie::set('Teacher',['赵老师','钱老师','孙老师','李老师']); dump($_COOKIE); } }
设置数组值为英文
<?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); } }
执行:
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'); } }
执行:
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_'); } }
执行:
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'); } }
执行:
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'); } }
执行:
判断含有前缀的值是否存在
<?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_'); } }
执行:
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); } }
执行:
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); } }
执行:
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); } }
执行:
清空前缀为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); } }
执行:
助手函数的使用可以查看手册
模板中使用cookie
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>
执行:
模板还可以这样写:
<p>网站名:{$_COOKIE['siteName']}</p>
执行:
裤颁阀内限壬赊次视禽痢脱巩