Heim  >  Artikel  >  Backend-Entwicklung  >  Yii中的cookie的发送和读取_php实例

Yii中的cookie的发送和读取_php实例

WBOY
WBOYOriginal
2016-08-17 13:02:41780Durchsuche

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')){}

以上所述是小编给大家介绍的Yii中的cookie的发送和读取 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn