Search code examples
phpajaxcookiessetcookie

Cookie via AJAX


I'm trying to set a cookie via AJAX directly with PHP. The code below sets the cookie just fine in Firefox, Opera and Safari, but not in Chrome and IE8.

`setcookie( "user" , $_REQUEST[ 'login' ] , time() + 60 * 60 * 60 , "/graduate/" , "localhost" );`

Am I missing something, or is the only way to do it is through JavaScript?

P.S. Using JavaScript the cookie is set in all browsers.


Solution

  • So i'm guessing youre doing this on a localhost? If so, its because google chrome has an issue with localhost sites and using the IP Address to set the cookies instead, more here: http://groups.google.com/a/googleproductforums.com/forum/#!category-topic/chrome/report-a-problem-and-get-troubleshooting-help/iow88FsnNhQ

    For internet Explorer, here is an excerpt from php site that should solve your problem: "Internet Explorer 8 and Internet Explorer 9 (IE8 and IE9) are still not accepting cookies from localhost.

    The simplest way around this is adding an entry to your hosts file e.g. 127.0.0.1 www.mytestsite.com"

    Here is the link: http://php.net/manual/en/function.setcookie.php

    Another issue is that some browsers dont work well unless you specify all the parameters of setcookie, try that too

    Some other links that may be of help: Internet Explorer ignores cookies on some domains (cannot read or set cookies)