简体   繁体   English

PHP-在Cookie到期时调用函数

[英]PHP - Call A Function On Cookie Expiry

I was just wondering if there is a pre-made system to call a function when a cookie expires. 我只是想知道Cookie过期时是否有预制的系统来调用函数。 I was wondering if there was something similar to: 我想知道是否有类似的东西:

setcookie('cookie', "Value!", time()+300, onexpire{/*Self destruct*/});

If not I can think of a different way to do it 如果没有,我可以想到另一种方式

As per knowledge there is no pre-made system for this. 据了解,没有为此的预制系统。 Actually its very difficult to achieve your requirement. 实际上,很难满足您的要求。

But you can create one more cookie that will store your first cookie expire time. 但是您可以再创建一个cookie,该cookie将存储您的第一个cookie过期时间。 And later on use second cookie to check expire time of first one and then call function. 然后使用第二个cookie来检查第一个cookie的到期时间,然后调用函数。

Hope so this way will help your. 希望这样可以对您有所帮助。

Cookies are stored in the client's browser. Cookies存储在客户端的浏览器中。 Browser will not tell the server when cookie expires. Cookie过期时,浏览器不会告诉服务器。

But there are some work-arounds: 但是有一些解决方法:

  1. Cron-job : Save a copy of cookies in your database, and set a cron-job to deal with them Cron-job:将Cookie的副本保存在数据库中,并设置cron-job来处理它们
  2. Ajax : Let client send an ajax request when cookie expires Ajax:当cookie过期时,让客户端发送ajax请求

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

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