简体   繁体   English

php的“ setcookie”不能跨浏览器工作吗?

[英]php's “setcookie” doesn't work cross-browser?

At the moment I'm using setcookie to set my cookies, however it only works in Firefox and Safari, and cookies are not set in IE and Chrome (and maybe other browsers). 目前,我正在使用setcookie设置cookie,但是它仅在Firefox和Safari中有效,并且未在IE和Chrome(以及可能的其他浏览器)中设置cookie。

setcookie($name, $value, time()+3600 * 25);

I read in an article about setcookie and IE, that if the expiry date is in the past (or is too small), IE simply ignores it. 我在一篇有关setcookie和IE的文章中读到,如果到期日期是过去的(或太短),则IE只会忽略它。

I know Javascript can set cookies (I'm using MooTools) but I'd prefer using php and MooTools & cookies isn't working for me. 我知道Javascript可以设置Cookie(我正在使用MooTools),但是我更喜欢使用php,而MooTools和Cookies对我不起作用。

Any help would be appreciated. 任何帮助,将不胜感激。

Check your server's clock. 检查服务器的时钟。 If it's running more than 25 hours behind, the time() + 3600 * 25 could still be in the past as far as the browser is concerned. 如果运行时间晚了25小时以上,那么就浏览器而言, time() + 3600 * 25可能仍然是过去的时间。

PHP can definitely set cookies cross browser - after all, it is just a header, and you don't see sessions failing in IE and Chrome on PHP sites do you? PHP绝对可以跨浏览器设置cookie-毕竟,它只是一个标头,您看不到PHP网站上的IE和Chrome中的会话失败吗?

I can understand why IE wouldn't honour a cookie with a past expiry date. 我可以理解,为什么IE不会使用过期日期的Cookie。

Have you examined the headers in those browsers? 您是否在这些浏览器中检查过标题?

setcookie doesn't depend on browser, the cookies are set using HTTP protocol headers , which is definitely cross-browser setcookie不依赖于浏览器,cookie是使用HTTP协议标头设置的,这肯定是跨浏览器的

The reason of setcookie fail may be: setcookie失败的原因可能是:

  • sending fome portion of text before setcookie executes. 在setcookie执行之前发送文本的fome部分。 Turn error messages on using error_reporting(E_ALL); 使用error_reporting(E_ALL);打开错误消息error_reporting(E_ALL); to see the line where the output started. 查看输出开始的行。 The cookies must be sent before the first line of HTML or the first echo executes. 必须在HTML的第一行或第一个echo执行之前发送cookie。
  • saving php files in UTF-8 encoding with BOM. 使用BOM将UTF-8编码的php文件保存。 BOM can be turned off. BOM可以关闭。 Open the file in a text editor, change encodind and re-save it. 在文本编辑器中打开文件,更改encodind并重新保存。 Create a backup first 首先创建备份
  • using setcookie with time which has been passed. 使用setcookie与已过去的时间。 The third parameter of setcookie is expiration time . setcookie的第三个参数是到期时间 Is less then time() , the cookies won't be saved 小于time() ,将不会保存cookie
  • specifying wrong parameters to setcookie . setcookie指定错误的参数。 Check your cookie_name parameter for containing alphanumeric cheracters only. 检查您的cookie_name参数仅包含字母数字化验者。 Do not specify cookie_path and cookie_domain at all. 完全不要指定cookie_pathcookie_domain

The solution is also using ob_start . 该解决方案还使用ob_start This function guarantees all the headers are sent successfully. 此功能确保所有标头均已成功发送。 Read the manual for details how to use it. 阅读手册以获取详细的使用方法。

This might help you. 这可能对您有帮助。 Might be applied to all IE's. 可能适用于所有IE。 I haven't tested it myself. 我自己还没有测试过。
http://php.net/manual/en/function.setcookie.php#100094 http://php.net/manual/zh/function.setcookie.php#100094

Here is the part from the page: 这是页面的一部分:

For those of your banging your head as to why a cookie is not present when Internet Explorer 6 prints, the explanation is quite interesting. 对于那些为什么在打印Internet Explorer 6时为什么不显示cookie的人,这种解释很有趣。 After a bit of investigation, a cookie with an expiration time other than 0 fails to be passed from IE6 to the server when printing. 经过一番调查后,打印时失效时间为0以外的cookie无法从IE6传递到服务器。 A cookie with an expiration time of 0 is sent. 将发送过期时间为0的Cookie。

Therefore: 因此:

setcookie("TestCookie", $value, time()+3600); setcookie(“ TestCookie”,$ value,time()+ 3600); //will not be sent from Print / Print Preview in IE6 //不会从IE6中的“打印” /“打印预览”发送

setcookie("TestCookie", $value, 0); setcookie(“ TestCookie”,$ value,0); //will be sent from Print / Print Preview in IE6 //将从IE6中的“打印” /“打印预览”发送

I'll let everyone figure out who's bright idea it was to not send normal expiring cookies when printing in IE6... 我会让每个人弄清楚谁是个好主意,那就是在IE6中打印时不发送正常的过期cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM