setcookie() - able to set cookies but not access through $_COOKIE
Posted by Vishal on August 14th, 2010Came across a strange problem today. I was setting a few cookies using the setcookie() and when trying to access them through $_COOKIE, they weren’t appearing in the array. The cookie is shown in browser but not accessible in $_COOKIE. Initially, I was just using:
setcookie(”cookie_name”,$cookie_value);
My script was residing in a folder ‘/abc’ on the server, which is present in the document root. Then I had to change the above to:
setcookie(”cookie_name”,$cookie_value, time()+(86400*365), “/”, “.example.com”, 1);
I put in the expiry date as 1 year from today, put in the path of the document root i.e. ‘/’ & also made the cookie to be available on all subdomains on my main domain (by using a .) rather than only on www.
The above line also didn’t solve the problem. Finally, I turned the last argument from ‘1′ to ‘0′. It specifies that the cookie will not be accessible by any scripting language & hence PHP was unable to access the cookie, though it was appearing in the browser cookies. What solved the problem was:
setcookie(”cookie_name”,$cookie_value, time()+(86400*365), “/”, “.example.com”, 0);
Hope this helps. ![]()






Recent Comments