简体   繁体   English

在一定的时间延迟后设置Cookie(在PHP中)

[英]Set cookie after certain time delay (in PHP)

I'd like to set a cookie in PHP for my website visitors after they have been on my site for at least 2 minutes. 在网站访问者访问我网站至少2分钟之后,我想用PHP设置cookie。

I guess the sleep() function could do just that, but I read that it might delay loading of the entire page. 我想sleep()函数可以做到这一点,但我读到它可能会延迟整个页面的加载。

Is there any other way to this? 还有其他方法吗?

You can create an ajax request from the JavaScript which will load a PHP file after 2 minutes. 您可以从JavaScript创建ajax请求,该请求将在2分钟后加载PHP文件。

In JS: 在JS中:

<script>
    setTimeout(function() {
        // create the AJAX request to set_the_cookie.php
    }, 120000);
</script>

Information about AJAX requests . 有关AJAX请求的信息。

In PHP (set_the_cookie.php): 在PHP(set_the_cookie.php)中:

<?php
    $value = 'yours_value';
    setcookie('cookie_name', $value);
?>

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

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