Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get cookie expiration

Tags:

php

cookies

Is it possible to read cookie expiration time with php ? When I print_r($_COOKIE) it outputs:

Array
(
    [PHPSESSID] => 0afef6bac83a7db8abd9f87b76838d7f
    [userId] => 1232
    [userEmail] => [email protected]
    [firstName] => user
    [lastName] => user
)

So I think $_COOKIE don't have the expiration time, is it possible with some other function?

like image 396
Ashish Rajan Avatar asked Jul 14 '26 20:07

Ashish Rajan


2 Answers

Only name and value are sent to the server so no other cookie data is available.

You can simply re-set the cookie if you want to extend its duration - that's just a few bytes more in the response so it doesn't matter at all.

like image 68
ThiefMaster Avatar answered Jul 16 '26 11:07

ThiefMaster


Or you can use the function time() on the value of the cookie, that way you need only one cookie and can retrieve data. The php code would look like this:

setCookie('cookiename', time(), time() + 86400);

That way, you'll have the cookie expiring in one day, and by retrieving it's value you can discover when it'll expire with something like this:

86400 - (time() - $_COOKIE['cookiename']);

like image 36
Vitor Speranza Avatar answered Jul 16 '26 10:07

Vitor Speranza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!