使用header(“ Set-cookie”)与setcookie()函数设置cookie [英] Setting cookie using header("Set-cookie") vs setcookie() function

查看:1175
本文介绍了使用header(“ Set-cookie”)与setcookie()函数设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重构一些代码,发现了一些我从未见过的东西。该函数用于用户登录时设置Cookie:

I'm refactoring some code and found something I've never seen. the function is used for user to set cookie when user logs in:

  function setUserCookie($name, $value) {
     $date = date("D, d M Y H:i:s",strtotime('1 January 2015')) . 'GMT';
     header("Set-Cookie: {$name}={$value}; EXPIRES{$date};");
  }

现在,我已被分配去重构计划使用的代码 setcookie 函数实际上根据php.net做同样的事情。

now that I've been assigned to refactor code I'm planning to use setcookie function which essentially does same thing according to php.net.

我的问题是:两者之间有什么区别吗?

My question is: is there any difference between two and which one should I use?

注意:这段代码是很久以前写的,所以我假设当时 setcookie 不存在?

NOTE: this code was written long time ago so I'm assuming that at that time setcookie didnt exist?

推荐答案

没有充分的理由不使用setcookie。上面的代码未正确编码名称和值,因此重构至少是一项主要好处。

There's no good reason not to use setcookie. The above code doesn't properly encode names and values, so that's at least one major benefit to refactoring.

这篇关于使用header(“ Set-cookie”)与setcookie()函数设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆