如何创建一个使脚本在后台运行的php脚本?

时间:2015-06-04 05:56:09

标签: php

我想创建一个在后台运行的函数。一个示例是从数据库中删除会话(idsession_tokensession_identifierexpired_time)。一旦过期,当前页面将自动将用户重定向回登录页面(login.php)。

更新

过期时间存储在数据库中。

2 个答案:

答案 0 :(得分:0)

Php Cronjob将是更好的方法。 Cronjob是一种在后台运行计划任务的方法。

请查看以下链接:

<强> http://www.thesitewizard.com/general/set-cron-job.shtml

答案 1 :(得分:0)

您可以使用jquery Ajax和setInterval函数

来执行此操作
 function check_login() {
            $.ajax({
                type: 'get',
                url: "http://stackoverflow.com", /*Where you will check user is logged in or not means session is expired or not*/
                beforeSend: function(xhr) {
                    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                },
                success: function(response) {
                    /*ajax call will return "Still login" (or any other response) if login*/
                    if (response == 'Still login') {
                        return true;
                    } else if (response == 'not login') {
                        window.location.replace("http://domain.com/login.php");
                    }
                }
            });
        }

        setInterval(check_login, 100);
相关问题