2

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

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

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

Ashish Rajan
  • 1,202
  • 5
  • 15
  • 27

3 Answers3

6

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.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 1
    No there isn't. What I personally do is save the expiration time in a database and have the cookie have some sort of identifier. Usually for saving login details the cookie is only the identifier (a long random one so it cannot be guessed). If the client (hypothetically) sent the expiration time, you shouldn't use it anyway, since it could have been tempered. – Artefacto May 15 '10 at 13:09
1

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']);

uday
  • 8,544
  • 4
  • 30
  • 54
  • that's clever. I will add the timer to the value of the cookie like "value:expire", and I will just have to split the value of the cookie to get the expire. Thanks ! – Fenix Aoras Oct 13 '22 at 17:19
1

no, there is no way.
Browser uses cookie parameters (path, expiration etc) only to determine to send a cookie or not, but none of these parameters being sent back to server.

don't think of a cookie as of a $_SESSON array member but as an HTTP header. That's always helps.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345