简体   繁体   English

Cookie有多个到期日期

[英]Cookie with multiple expiration dates

I am setting a cookie, with a 60 day expiration time. 我正在设置一个cookie,有效期为60天。 The cookie sets which TOWN the user has selected for our site. cookie设置用户为我们的网站选择的TOWN。 We also want to have a popup window open upon page loading ... but want that to happen every week (and have the cookie check if "it's been over two weeks"). 我们还希望在页面加载时打开一个弹出窗口……但是希望每周发生一次(并让Cookie检查“是否已经超过两周”)。

We are trying to avoid setting TWO cookies. 我们试图避免设置两个cookie。 I assume I can do this by checking how long 'til the cookie expires, then figure out when the start date was (based on the $number_of_days), and then calculate 2 weeks from the start date, and THEN see if the date/time has passed. 我假设我可以通过以下步骤来检查Cookie的过期时间,然后确定开始日期(基于$ number_of_days),然后从开始日期开始计算2周,然后查看日期/时间已经过去了。 However, my php knowledge is pretty limited. 但是,我的PHP知识非常有限。 Wondering if anyone has done this before and can show me how... I just need a variable outputted states whether 2 weeks has passed or not (ie $openpopup=1); 想知道是否有人以前做过,并且可以告诉我如何...我只需要输出变量状态是否已经过去了2周(即$ openpopup = 1);

I am using the following code for my cookie (single expiration date)... 我正在将以下代码用于cookie(单个有效期)...

$number_of_days = 60 ;
$date_of_expiry = time() + 60 * 60 * 24 * $number_of_days ;
if(isset($town)) { 
    // Set Cookies
    setcookie( "towns", $town, $date_of_expiry, "/" ); 
} else {
    // Del Cookies
    unset($_COOKIE['towns']);
    // empty value and expiration one hour before
    setcookie('towns', '', time() - 3600);
}

You cannot read the expiration date back once the cookie has been set. 设置cookie后,您将无法回读到期日期。 You can only get the key=value pairs. 您只能获取键=值对。 If you need the date then you need to set it as part of the data in the cookie itself. 如果需要日期,则需要将其设置为cookie本身数据的一部分。

You could set multiple pieces of data in an array and the serialize it and store it in the cookie. 您可以在一个数组中设置多个数据并将其序列化并将其存储在cookie中。 Then you would unserialize it when you read it back. 然后,当您读回它时,将会反序列化它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM