FOUND THE ISSUE, see below at the end
I have a php page in wich I save a cookie :
include "scripts/mytoolkit.php";
...
if ($val != "") saveCookie('mycookie', $val, 100*365, "www.domain.com"); // tested, $val is not empty and is a string. Want to keep the cookie for 100 years :-))
...
with mytoolkit.php
function saveCookie($name, $value, $days, $domain)
{
$expires = 0;
// tested, $name and $value are correct and filled
if (isset($days))
$expires = time()+$days*24*60*60; // tested, the method enters there
setcookie ($name, $value, $expires, "/", $domain);
}
function readCookie($name)
{
if(isset($_COOKIE[$name])) return $_COOKIE[$name];
return null;
}
Later, after having reloaded the page, I check the cookie and print it :
...
$val = readCookie('mycookie');
print ($val);
...
The problem is that $val is empty, and of course the cookie is not set.
BUT...
if I just replace the call :
saveCookie('mycookie', $val, 100*365, "www.domain.com");
by the following :
setcookie ('mycookie', $val, time()+100*365, "/", "www.domain.com");
or by keeping
saveCookie('mycookie', $val, 100*365, "www.domain.com");
but removing *24*60*60;
from the saveCookie
method
Then everything works fine and $val is filled with the cookie value, and the cookie is well set.
What is going wrong with the use of the method to set the cookie ?
ISSUE
The issue was the fact I wanted to keep the cookie 100 years. If I put a more limited delay, like 10 years, it works fine.
The question is : why can I use 100 years in javascript when I set a cookie, and why can't I do the same using PHP ?
From http://www.php.net/set_cookie:
The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
The problem maybe is because you're trying to set an expire timestamp that exceed the unix timestamp limit AND your php is a 32 bits version. The limit of the unix timestamp on system that uses a 32 bits int is January 19, 2038.
From http://www.unixtimestamp.com/index.php:
What happens on January 19, 2038?
On this date the Unix Time Stamp will cease to work due to a 32-bit overflow. Before this moment millions of applications will need to either adopt a new convention for time stamps or be migrated to 64-bit systems which will buy the time stamp a "bit" more time.