Yii中的cookie的发送和读取
cookies:
//新创建的cookie会从本地传到服务器上,然后从服务器获取。
(1) cookie的发送
$cookies = Yii::$app->response->cookies; // 在要发送的响应中添加一个新的cookie eg:往cookies中添加用户名和密码 $cookies->add(new Cookie(['name'=>'username', 'value'=>$username,])); $cookies->add(new Cookie(['name'=>'password', 'value'=>$password,])); //删除一个cookie $cookies->remove('username'); //相当于 unset($cookies['username']);
(2)cookie的获取
$cookies = Yii::$app->request->cookies; // 获取名为 "username" cookie 的值,如果不存在,返回默认值"en" $username = $cookies->getValue('username', 'en'); // 另一种方式获取名为 "username" cookie 的值 if (($cookie = $cookies->getValue('username')) !== null) { $username = $cookie->value; } // 判断是否存在名为username的cookie if (isset($cookies['username'])) { $username= $cookies['username']->value; ... }if($cookies->has('username')){}
相关推荐
houmenghu 2020-11-17
kentrl 2020-11-10
逍遥友 2020-10-26
jincheng 2020-09-01
Blueberry 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
阳光之吻 2020-08-03
PkJY 2020-07-08
hzyuhz 2020-07-04
89407707 2020-06-27
服务器端攻城师 2020-06-26
阳光岛主 2020-06-25
笨重的蜗牛 2020-06-20
xuanwenchao 2020-06-14
Lophole 2020-06-13
明瞳 2020-06-12
songerxing 2020-06-11