Php 会话超时和自动重定向

Php 会话超时和自动重定向,php,session,Php,Session,当用户单击一个链接并进入下一页时,它会启动一个会话,长话短说,告诉数据库使该链接不可用。在数据库重置使链接再次可用之前,他们只有30分钟的时间完成此页面上的任务。。我如何才能使用户不能坐在页面上保持链接不可用或单击“刷新”保持在同一页面上 那么基本上,有没有一种方法可以让用户在不点击任何东西的情况下自动重定向到另一个页面?当会话过期时,无论发生什么情况,页面都应该将它们重定向到另一个页面 我想我不能使用这个,因为我希望重定向取决于会话何时到期。 header("Refresh: 60; Loca

当用户单击一个链接并进入下一页时,它会启动一个会话,长话短说,告诉数据库使该链接不可用。在数据库重置使链接再次可用之前,他们只有30分钟的时间完成此页面上的任务。。我如何才能使用户不能坐在页面上保持链接不可用或单击“刷新”保持在同一页面上

那么基本上,有没有一种方法可以让用户在不点击任何东西的情况下自动重定向到另一个页面?当会话过期时,无论发生什么情况,页面都应该将它们重定向到另一个页面

我想我不能使用这个,因为我希望重定向取决于会话何时到期。

header("Refresh: 60; Location: /path/somepage.php");
任何帮助都会非常有用

**编辑,则在会话中定义30分钟。所以,这一切都是关于课程的

$now = time();

$_SESSION['start'] = time(); // taking now page start time
$_SESSION['expire'] = $_SESSION['start'] + (1 * 60) ; // ending a session in 30

$outOfTime = $_SESSION['expire'] - $_SESSION['start'];

if($now > $_SESSION['expire'])
{

    header("Refresh: $outOfTime ; Location: /path/redirect.php");

}

出于测试目的,会话计时器设置为1分钟。

您应该有一个系统来检查每次用户进入页面时运行的会话过期时间,并将重定向设置为该时间。例如:

<?php
#some code to get expiration date of session. Make sure it is in a datetime object.
#get difference between current time and expiration time:
$timeleft=$expirationTime-new DateTime('now');
header("Refresh: {$timeleft};Location: http://example.com");
?>


我对日期/时间不太在行,因此您可能需要通过代码来解决这个问题,但这个概念是这个答案的主要目的。

我想让您考虑的任何方式

滴答声

勾号是解析器在declare块中执行的每N个低级语句发生的事件。N的值是使用declare blocks的指令部分中的ticks=N指定的。 使用register_tick_函数()指定每个tick上发生的事件。有关更多详细信息,请参见下面的示例。请注意,每个刻度可能会发生多个事件

register\u tick\u函数——注册一个函数以在每个 滴答声

注册由func命名的函数,以便在调用记号时执行。此外,还可以将由对象和方法组成的数组作为func传递

register_tick_function()示例


警告 register_tick_function()不应与线程化Web服务器模块一起使用。滴答声无法在ZTS模式下工作,可能会使您的Web服务器崩溃。

我只是从我的架子上抄的,希望它能帮助我们社区的任何一个人

<?php 
     /**
     *  ************************ NOTICE ***********************************
     *
     *      The use of Timers WILL slow down your script execution time.
     *      By how much is determined by the user implementation.
     *
     *  *******************************************************************
     *
     *  This pacakge contains one class for handling timers in PHP
     *  and enables the handling of callback functions at given intervals
     *  in microseconds (NOT milliseconds like javascript), as well as 
     *  removing said functions from the stack of callable functions.
     *  
     *  The class is dependent on the PHP language construct declare(ticks=N);
     *  where N represents how many "tickable statements" that get processed
     *  before a registered tick function is called and MUST be declared in the top level script, 
     *  not an included file in order to be effective. 
     *
     *  @see http://us.php.net/manual/en/control-structures.declare.php#control-structures.declare.ticks
     *  
     *  The value of N determines
     *  1) how close to perfect accuracy the timers are (probably never be perfect though)
     *  2) how fast the script will be processed
     *  If N == 1 the script will be very close to perfectly accurate, but will run very slow
     *  but if N is set TOO high (like 10000) it may not be very effective or accurate.
     *  It is up to the user to determine what this number should be for their script.
     *
     *  The package also includes 4 functions for simplifying calls to the static methods of the class:
     *      -- setTimeout, setInterval, clearTimeout, clearInterval

    /**
     *  Just for simplifying the Timers::setTimeout method
     *
     *
     *  @param callable | string $func
     *  @param integer $microseconds - remember this is microseconds NOT milliseconds
     *
     *  @return integer
     */
    function setTimeout ($func, $microseconds)
    {
        return Timers::setTimeout($func, $microseconds);
    }

    /**
     *  Just for simplifying the Timers::setInterval method
     *
     *
     *  @param callable | string $func
     *  @param integer $microseconds - remember this is microseconds NOT milliseconds
     *
     *  @return integer
     */
    function setInterval ($func, $microseconds)
    {
        return Timers::setInterval($func, $microseconds);
    }

    /**
     *  Just for simplifying the Timers::clearTimeout method
     *
     *
     *  @param integer $interval - an integer representing the one returned from a call to setTimeout()
     *
     *  @return boolean
     */
    function clearTimeout ($func, $microseconds)
    {
        return Timers::setTimeout($func, $microseconds);
    }

    /**
     *  Just for simplifying the Timers::clearInterval method
     *
     *
     *  @param integer $interval - an integer representing the one returned from a call to setInterval()
     *
     *  @return boolean
     */
    function clearInterval ($interval)
    {
        return Timers::clearInterval($interval);
    }

    /**
     *  This class contains a series of static properties and functions
     *  that enable the creation and execution of timers
     *
     *  @author Sam Shull
     */
    class Timers
    {
        /**
         *  An array of the arrays that represent
         *  the timer information used by Timers::tick
         *
         *  @access private
         *  @staticvar array
         */
        private static $timers = array();

        /**
         *  Tracker of timers
         *
         *
         *  @access private
         *  @staticvar integer
         */
        private static $numTimers = 0;

        /**
         *  An array of the arrays that represent
         *  the interval information used by Timers::tick
         *
         *  @access private
         *  @staticvar array
         */
        private static $intervals = array();

        /**
         *  Tracker of intervals
         *
         *
         *  @access private
         *  @staticvar integer
         */
        private static $numIntervals = 0;

        /**
         *  Used for debugging
         *
         *
         *  @access private
         *  @staticvar integer
         */
        //private static $ticks = 0;

        /**
         *  A utility method called after N number of ticks by the engine
         *  that checks each timer and interval to see if the desired 
         *  number of microseconds have passed and executes the function 
         *  when appropriate
         *
         *  @static
         *  @return void
         */
        public static function tick ()
        {
            //++self::$ticks;

            $time = self::microtime();

            foreach (self::$timers as $position => $timer)
            {
                if ($time >= $timer['time'])
                {
                    call_user_func($timer['function']);
                    unset(self::$timers[$position]);
                }
            }

            foreach (self::$intervals as $position => $timer)
            {
                if ($time >= $timer['time'])
                {
                    call_user_func($timer['function']);
                    self::$intervals[$position]['time'] = self::microtime() + self::$intervals[$position]['microseconds'];
                }
            }
        }

        /**
         *  A utility method for retrieving the most accurate
         *  microtime available
         *
         *  @static
         *  @return float
         */
        public static function microtime ()
        {
            list($m, $s) = explode(' ', microtime());
            return round(((float)$m + (float)$s) * 1000000);
        }

        /**
         *  A utility method that ensures that all the timeouts have been called
         *  and that calls all the intervals one more time
         *
         *
         *  @static
         *  @return void
         */
        public static function shutdown ()
        {
            foreach (self::$timers as $position => $timer)
            {
                call_user_func($timer['function']);
                unset(self::$timers[$position]);
            }

            foreach (self::$intervals as $position => $interval)
            {
                call_user_func($interval['function']);
                unset(self::$intervals[$position]);
            }

            //print "\nticks: " . self::$ticks;
        }

        /**
         *  Add a function to the be executed after ($microseconds) microsecond
         *
         *  @static
         *
         *  @param callable | string $func
         *  @param integer $microseconds - remember microseconds, not miliseconds
         *
         *  @return integer
         */
        public static function setTimeout ($func, $microseconds)
        {
            if (!is_callable($func))
            {
                if (is_string($func))
                {
                    $func = create_function('', $func);
                }
                else
                {
                    throw new InvalidArgumentException();
                }
            }

            self::$timers[++self::$numTimers] = array(
                                                        'time' => self::microtime() + $microseconds, 
                                                        'function' => $func,
                                                    );

            return self::$numTimers;
        }

        /**
         *  Add a function to the be executed every ($microseconds) microsecond
         *
         *  @static
         *
         *  @param callable | string $func
         *  @param integer $microseconds - remember microseconds, not miliseconds
         *
         *  @return integer
         */
        public static function setInterval ($func, $microseconds)
        {
            if (!is_callable($func))
            {
                if (is_string($func))
                {
                    $func = create_function('', $func);
                }
                else
                {
                    throw new InvalidArgumentException();
                }
            }

            self::$intervals[++self::$numIntervals] = array(
                                                                'time' => self::microtime() + $microseconds, 
                                                                'function' => $func,
                                                                'microseconds' => $microseconds,
                                                            );

            return self::$numIntervals;
        }

        /**
         *  Remove a timeout function from the stack
         *
         *  @static
         *
         *  @param integer $timer
         *
         *  @return boolean
         */
        public static function clearTimeout ($timer)
        {
            if (isset(self::$timers[$timer]))
            {
                unset(self::$timers[$timer]);
                return true;
            }

            return false;
        }

        /**
         *  Remove an interval function from the stack
         *
         *  @static
         *
         *  @param integer $interval
         *
         *  @return boolean
         */
        public static function clearInterval ($interval)
        {
            if (isset(self::$intervals[$interval]))
            {
                unset(self::$intervals[$interval]);
                return true;
            }

            return false;
        }
    }

    /**
     *  Register these methods in order to perform polling a specific intervals
     *  that are set by the user
     */
    register_tick_function(array('Timers','tick'));
    register_shutdown_function(array('Timers','shutdown'));

    ?>

因为硬刷新页面是不可取的(对用户来说也不好!),所以您必须提供javascript来定期查询侦听器,该侦听器报告页面上剩余的时间,或页面过期的unix日期时间

在受限页面的顶部:

session_start();
if (!isset($_SESSION['page_expiry']) || time() < $_SESSION['page_expiry'])
    $_SESSION['page_expiry'] = time() + (60 * 30);
    // render page
} else {
    echo "time's up!";
}

如果ajax调用返回false,将其踢出页面。

使用JavaScript超时fn这并不能解决@DBroncos1558存在的问题,因为用户只需坐在页面上超过超时。@Daedalus标头标记在$timeleft秒后重定向用户,因此,用户不能整天坐在页面上。这与他所说的标题(“Refresh:60;Location:/path/somepage.php”)中的问题有什么区别@IOIOMAD它是不同的,因为它将秒数设置为会话到期前的剩余时间,而不是固定时间value@Markasoftware阅读这个,看起来Javascript是唯一的方法,没有办法绕过它。如果Javascript在他们的浏览器上被禁用,它不会工作,是吗?没错,它不会工作。如果您确实需要限制用户,那么当他们在受限页面上“提交”时,您可以检查会话是否已过期。如果有,则使result.ahhh dang Javascript无效,该Javascript非常有用,但容易受到安全漏洞的攻击。是的,我有它,所以当他们点击提交,页面上说会话已过期,请重新启动。我只是想阻止有人离开页面,从而保持链接不可用或刷新链接并重新启动会话。如果用户单击“刷新”按钮,我可以阻止会话重新启动吗?谢谢您的回复!我对蜱没有任何线索,从阅读这篇文章来看,它可能非常有用。我的最后期限快到了,否则我会进一步研究如何使用这个。。再次感谢,我希望你的回答能帮助别人!
<?php 
     /**
     *  ************************ NOTICE ***********************************
     *
     *      The use of Timers WILL slow down your script execution time.
     *      By how much is determined by the user implementation.
     *
     *  *******************************************************************
     *
     *  This pacakge contains one class for handling timers in PHP
     *  and enables the handling of callback functions at given intervals
     *  in microseconds (NOT milliseconds like javascript), as well as 
     *  removing said functions from the stack of callable functions.
     *  
     *  The class is dependent on the PHP language construct declare(ticks=N);
     *  where N represents how many "tickable statements" that get processed
     *  before a registered tick function is called and MUST be declared in the top level script, 
     *  not an included file in order to be effective. 
     *
     *  @see http://us.php.net/manual/en/control-structures.declare.php#control-structures.declare.ticks
     *  
     *  The value of N determines
     *  1) how close to perfect accuracy the timers are (probably never be perfect though)
     *  2) how fast the script will be processed
     *  If N == 1 the script will be very close to perfectly accurate, but will run very slow
     *  but if N is set TOO high (like 10000) it may not be very effective or accurate.
     *  It is up to the user to determine what this number should be for their script.
     *
     *  The package also includes 4 functions for simplifying calls to the static methods of the class:
     *      -- setTimeout, setInterval, clearTimeout, clearInterval

    /**
     *  Just for simplifying the Timers::setTimeout method
     *
     *
     *  @param callable | string $func
     *  @param integer $microseconds - remember this is microseconds NOT milliseconds
     *
     *  @return integer
     */
    function setTimeout ($func, $microseconds)
    {
        return Timers::setTimeout($func, $microseconds);
    }

    /**
     *  Just for simplifying the Timers::setInterval method
     *
     *
     *  @param callable | string $func
     *  @param integer $microseconds - remember this is microseconds NOT milliseconds
     *
     *  @return integer
     */
    function setInterval ($func, $microseconds)
    {
        return Timers::setInterval($func, $microseconds);
    }

    /**
     *  Just for simplifying the Timers::clearTimeout method
     *
     *
     *  @param integer $interval - an integer representing the one returned from a call to setTimeout()
     *
     *  @return boolean
     */
    function clearTimeout ($func, $microseconds)
    {
        return Timers::setTimeout($func, $microseconds);
    }

    /**
     *  Just for simplifying the Timers::clearInterval method
     *
     *
     *  @param integer $interval - an integer representing the one returned from a call to setInterval()
     *
     *  @return boolean
     */
    function clearInterval ($interval)
    {
        return Timers::clearInterval($interval);
    }

    /**
     *  This class contains a series of static properties and functions
     *  that enable the creation and execution of timers
     *
     *  @author Sam Shull
     */
    class Timers
    {
        /**
         *  An array of the arrays that represent
         *  the timer information used by Timers::tick
         *
         *  @access private
         *  @staticvar array
         */
        private static $timers = array();

        /**
         *  Tracker of timers
         *
         *
         *  @access private
         *  @staticvar integer
         */
        private static $numTimers = 0;

        /**
         *  An array of the arrays that represent
         *  the interval information used by Timers::tick
         *
         *  @access private
         *  @staticvar array
         */
        private static $intervals = array();

        /**
         *  Tracker of intervals
         *
         *
         *  @access private
         *  @staticvar integer
         */
        private static $numIntervals = 0;

        /**
         *  Used for debugging
         *
         *
         *  @access private
         *  @staticvar integer
         */
        //private static $ticks = 0;

        /**
         *  A utility method called after N number of ticks by the engine
         *  that checks each timer and interval to see if the desired 
         *  number of microseconds have passed and executes the function 
         *  when appropriate
         *
         *  @static
         *  @return void
         */
        public static function tick ()
        {
            //++self::$ticks;

            $time = self::microtime();

            foreach (self::$timers as $position => $timer)
            {
                if ($time >= $timer['time'])
                {
                    call_user_func($timer['function']);
                    unset(self::$timers[$position]);
                }
            }

            foreach (self::$intervals as $position => $timer)
            {
                if ($time >= $timer['time'])
                {
                    call_user_func($timer['function']);
                    self::$intervals[$position]['time'] = self::microtime() + self::$intervals[$position]['microseconds'];
                }
            }
        }

        /**
         *  A utility method for retrieving the most accurate
         *  microtime available
         *
         *  @static
         *  @return float
         */
        public static function microtime ()
        {
            list($m, $s) = explode(' ', microtime());
            return round(((float)$m + (float)$s) * 1000000);
        }

        /**
         *  A utility method that ensures that all the timeouts have been called
         *  and that calls all the intervals one more time
         *
         *
         *  @static
         *  @return void
         */
        public static function shutdown ()
        {
            foreach (self::$timers as $position => $timer)
            {
                call_user_func($timer['function']);
                unset(self::$timers[$position]);
            }

            foreach (self::$intervals as $position => $interval)
            {
                call_user_func($interval['function']);
                unset(self::$intervals[$position]);
            }

            //print "\nticks: " . self::$ticks;
        }

        /**
         *  Add a function to the be executed after ($microseconds) microsecond
         *
         *  @static
         *
         *  @param callable | string $func
         *  @param integer $microseconds - remember microseconds, not miliseconds
         *
         *  @return integer
         */
        public static function setTimeout ($func, $microseconds)
        {
            if (!is_callable($func))
            {
                if (is_string($func))
                {
                    $func = create_function('', $func);
                }
                else
                {
                    throw new InvalidArgumentException();
                }
            }

            self::$timers[++self::$numTimers] = array(
                                                        'time' => self::microtime() + $microseconds, 
                                                        'function' => $func,
                                                    );

            return self::$numTimers;
        }

        /**
         *  Add a function to the be executed every ($microseconds) microsecond
         *
         *  @static
         *
         *  @param callable | string $func
         *  @param integer $microseconds - remember microseconds, not miliseconds
         *
         *  @return integer
         */
        public static function setInterval ($func, $microseconds)
        {
            if (!is_callable($func))
            {
                if (is_string($func))
                {
                    $func = create_function('', $func);
                }
                else
                {
                    throw new InvalidArgumentException();
                }
            }

            self::$intervals[++self::$numIntervals] = array(
                                                                'time' => self::microtime() + $microseconds, 
                                                                'function' => $func,
                                                                'microseconds' => $microseconds,
                                                            );

            return self::$numIntervals;
        }

        /**
         *  Remove a timeout function from the stack
         *
         *  @static
         *
         *  @param integer $timer
         *
         *  @return boolean
         */
        public static function clearTimeout ($timer)
        {
            if (isset(self::$timers[$timer]))
            {
                unset(self::$timers[$timer]);
                return true;
            }

            return false;
        }

        /**
         *  Remove an interval function from the stack
         *
         *  @static
         *
         *  @param integer $interval
         *
         *  @return boolean
         */
        public static function clearInterval ($interval)
        {
            if (isset(self::$intervals[$interval]))
            {
                unset(self::$intervals[$interval]);
                return true;
            }

            return false;
        }
    }

    /**
     *  Register these methods in order to perform polling a specific intervals
     *  that are set by the user
     */
    register_tick_function(array('Timers','tick'));
    register_shutdown_function(array('Timers','shutdown'));

    ?>
session_start();
if (!isset($_SESSION['page_expiry']) || time() < $_SESSION['page_expiry'])
    $_SESSION['page_expiry'] = time() + (60 * 30);
    // render page
} else {
    echo "time's up!";
}
session_start();
if (time() > $_SESSION['page_expiry']) echo 'false';
else echo true;