简体   繁体   中英

setcookie() in example.com, cookie not found in www.example.com

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 ?

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.

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