Setcookie () in example.com, cookie not found on www.example.com

I downloaded http://example.com containing:

<?php 
    setcookie("mycookie", "hello", time() + 3600 * 24 * 31);

Then the entry document.cookiein the Javascript browser console shows the cookie. It works. Then I close and reopen the browser and go to http://www.example.com . Then the entry document.cookiein the Javascript console does not display a cookie.

How to change this PHP code to make the cookie shared between http://example.com and http://www.example.com ?

+4
source share
1 answer

Please correct the code as follows:

<?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
http://example.com/*, http://www.example.com/* .

+1

Source: https://habr.com/ru/post/1685084/


All Articles