Приглашаем посетить
Спорт (www.sport-data.ru)

Setting a (Reasonable) Expiry Date

Previous
Table of Contents
Next

Setting a (Reasonable) Expiry Date

The expiry date of a cookie is the third parameter for setcookie(). It is an integer value; therefore, the epoche value for a time stamp must be used. Chapter 3, "Date and Time," contains quite a lot of information on how to work with this type of information.

Setting a Cookie with a Relative Expiry Date (setcookie-expiry.php)
<?php
  setcookie('version', phpversion(), time() +
    21*24*60*60);
?>
Tried to send cookie.

Usually, it is a good thing to set a relative expiry date for a cookie ("in three weeks") rather than an absolute date ("end of May 2006"). If you use absolute dates, you might have to change your script on a regular basis because the absolute expiry date might arrive soon. It is also considered very unprofessional to set expiry dates that are in the very distant future, for instance in the year 2030. The client that receives this cookie will most certainly not be booted any more in that year.

Therefore, use a relative date. The PHP function time() retrieves the current epoche value; then add to this the number of seconds you want the cookie to live. The code at the beginning of this phrase sets a cookie that will exist for three weeks.


Previous
Table of Contents
Next