设置Cookie
PHP (推荐学习:yii教程)
setcookie("name", "Larry", time()+3600);
Yii2
$cookies = Yii::$app->response->cookies; $cookies->add(new \yii\web\Cookie([ 'name' => 'name', 'value' => 'Larry', 'expire'=>time()+3600 ]));
创建名为"name"的cookie,赋值为"Larry",并且规定该cookie一小时后失效
expire这是一个UNIX时间戳,如果设置为0,或省略,该Cookie将在浏览器关闭时消失
获取Cookie
PHP
$name=$_COOKIE["user"]
Yii2
$cookies = Yii::$app->request->cookies;//注意此处是request $language = $cookies->get('user', 'defaultName');//获取默认值
检查Cookie
PHP
if(isset($_COOKIE["user"])){ }
Yii2
$cookies = Yii::$app->request->cookies; if (isset($cookies['user'])){ } if ($cookies->has('user')){ } if (isset($cookies['user'])){ }
以上是yii2怎么查看cookies的详细内容。更多信息请关注PHP中文网其他相关文章!