Displaying date a cookie expires in php

Asked

Viewed 198 times

1

Currently I serve my cookie like this:

setcookie("visita","sim", time() + 172800,  $path = "/"); // 86400 = 1 dia, 172800 = 2 dias (SEGUNDOS)

But how can I show on a php page like this:

Your "visit" cookie expires on : 15/20/2050

KIND OF WHAT THE BROWSER DOES:

inserir a descrição da imagem aqui

1 answer

3


There is no way to get this information because it is not sent to the server.


When the server sends a cookie to the user (SERVER -> CLIENT) the header of Set-Cookie, it has some parameters like listed here, the attribute of expires is one of them.

However, on the contrary this is not done. When the customer requests the site he uses the header of Cookie, it contains only the information of the nome and valor of the cookie, but not the other data.


Therefore:

== Servidor -> Cliente ==

Set-Cookie: lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT

== Cliente -> Servidor ==

Cookie: lang=en-US

PHP only has the information of lang=en-US, the expiration data the client does not report (since this value has already been informed by the server at a previous time).


An alternative is to include the "expiry" date within the cookie itself, or store the "expiry" time when using the Set-Cookie.

In this case you could use:

$t = time() + 172800;
setcookie("visita", $t, $t,  $path = "/");

That way, when to use the $_COOKIE['visita'] will have the value that is "the same" as the expiration term.

  • Thanks! has how to transform the value that appears in date ?

  • You can format using date.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.