Reputation: 46411
I loaded http://example.com containing:
<?php
setcookie("mycookie", "hello", time() + 3600 * 24 * 31);
Then writing document.cookie
in the browser's Javascript console shows the cookie. It works. Then I close and reopen the browser and go to http://www.example.com. Then writing document.cookie
in the Javascript console doesn't show any cookie.
How to modify this PHP code to make the cookie shared between http://example.com and http://www.example.com?
Upvotes: 4
Views: 73
Reputation: 194
Please correct the code like this -
<?php
setcookie("mycookie", "hello", time() + 3600 * 24 * 31, "/", ".example.com");
?>
This slash (/) might trigger both WWW and non WWW and also every page of the site
It might work for http://example.com/*
and also http://www.example.com/*
It might work.
Upvotes: 1