简体   繁体   中英

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($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.

I know Javascript can set cookies (I'm using MooTools) but I'd prefer using php and MooTools & cookies isn't working for me.

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.

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?

I can understand why IE wouldn't honour a cookie with a past expiry date.

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

The reason of setcookie fail may be:

  • sending fome portion of text before setcookie executes. Turn error messages on using 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.
  • saving php files in UTF-8 encoding with BOM. BOM can be turned off. Open the file in a text editor, change encodind and re-save it. Create a backup first
  • using setcookie with time which has been passed. The third parameter of setcookie is expiration time . Is less then time() , the cookies won't be saved
  • specifying wrong parameters to setcookie . Check your cookie_name parameter for containing alphanumeric cheracters only. Do not specify cookie_path and cookie_domain at all.

The solution is also using 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. I haven't tested it myself.
http://php.net/manual/en/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. 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. A cookie with an expiration time of 0 is sent.

Therefore:

setcookie("TestCookie", $value, time()+3600); //will not be sent from Print / Print Preview in IE6

setcookie("TestCookie", $value, 0); //will be sent from Print / Print Preview in IE6

I'll let everyone figure out who's bright idea it was to not send normal expiring cookies when printing in IE6...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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