Приглашаем посетить
Бианки (bianki.lit-info.ru)

Setting a Client-Specific Expiry Date

Previous
Table of Contents
Next

Setting a Client-Specific Expiry Date

setcookie('version', phpversion(),
     $_GET['time'] + 21*24*60*60)


Keep in mind that the decision of when a cookie expires is made on the client side, using the client time settings. So, if the client has the wrong date (which is something you cannot control from the server-side), your cookies might expire sooner than you expect. So, don't try to set cookies that live for just one hour or so; small things like DST could destroy your plan.

Setting a Cookie with a Specific Expiry Date (setcookie-specific.php)
<?php
  if (isset($_GET['time']) && is_int($_GET['time']))
   {
    setcookie('version', phpversion(),
      $_GET['time'] + 21*24*60*60);
  } else {
    setcookie('version', phpversion(),
      time() + 21*24*60*60);
  }
?>
Tried to send cookie.

However, with a bit of JavaScript, it can be possible to avoid this trap. The code in the following listing is client-side JavaScript code that determines the current time (client-side!) as an epoche value and sends it to a server-side script, setcookie-specific.php. The main difference between PHP's time() function and JavaScript's getTime() method is that the latter returns the number of milliseconds since January 1, 1970, whereas the former just works with the number of seconds. So, the JavaScript value first has to be divided by 1,000 and rounded down.

The code for this file can be seen at the beginning of this phrase: The transmitted value is taken as the basis for the calculation of the relative cookie expiration date.

<script language="JavaScript"
  type="text/javascript"><!
  var epoche = (new Date()).getTime();
  epoche = Math.floor(epoche / 1000);
  location.replace("setcookie-specific.php?time=" +
    epoche);
//></script>


Previous
Table of Contents
Next