A site we are using is running an older HHVM version.
Looking at the documentation of PHP function setcookie(), there is two signatures for parameters.
To summarize the issue why I cannot use setcookie()
is because this version does not use the $options
array available of PHP7.3. When trying to use some of the alternative solutions by concatenating samesite
to path
will crash HHVM. Using this alternate method using normal PHP works correctly as expected.
There seems to be a slight difference between HHVM behaviour here with cookies compared to PHP.
So this question is about header() and not about setcookie() because I can't use it, there are slight difference in how HHVM (the version running) handles cookies.
NOTE: This is a Magento 1 site --- and upgrading to HHVM 3.30+ breaks everything so that is also not an option - I have tried this already.
So I managed to set the cookie using header()
function by concatenating all the properties.
header('Set-Cookie: frontend=abcdef; expires=188888888; path=/; domain=www.mydomain.com; SameSite=None; Secure');
Result in Response Headers:
frontend=abcdef; expires=188888888; path=/; domain=www.mydomain.com; Secure; SameSite=None
Set-Cookie documentation refers to this:
You may notice the expires parameter takes on a Unix timestamp, as opposed to the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally.
Question
How to correctly set the expires value, as I am creating a raw header instead of using setcookie()
the UNIX timestamp will be in the cookie as shown above, in other words setcookie()
converts the UNIX timestamp internally...
I have tried the following as well - but I am not sure this is correct for expires
or not: What format is required?
Sample:
php -a
php > $b = 3600 * 24 * 365;
php > $c = time() + $b;
php > echo $c;
1643355613
php > $dt = new DateTime();
php > $dt->setTimestamp($c);
php > echo $dt->format('Y-m-d H-i-s');
2022-01-28 09-40-13
Not entirely sure that format will be correctly used by expires
This question relates to issues about sessions not working in some cases, but I will keep these questions seperate.
question from:
https://stackoverflow.com/questions/65935670/php-correctly-set-cookie-parameters-expire-using-header-not-set-cookie 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…