如何在 PHP 中设置会话超时代码 [英] How to set session timeout code in PHP

查看:55
本文介绍了如何在 PHP 中设置会话超时代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一页 session.php,我的代码是:

I have one page session.php and my code is:

 @session_start();
            if(!isset($_SESSION['uname']) || $_SESSION['uname'] == " " || !isset($_SESSION['uid']) || $_SESSION['uid'] == " ")
                   {
                echo "<script language='javascript'>";
                echo "window.location='../index.php'";
                echo "</script> ";
                exit;           
            }

而且我还想编辑会话超时函数,这段代码是:

And I also want to edit session timeout function this code is:

$_SESSION['start'] = time(); // taking now logged in time
        $_SESSION['expire'] = $_SESSION['start'] + (1* 10) ; // ending a session in 30 seconds
            $now = time(); // checking the time now when home page starts
            if($now > $_SESSION['expire'])
            {
                session_destroy();
                echo "Your session has expire !  <a href='logout.php'>Click Here to Login</a>";
            }
            else
            {
                echo "This should be expired in 1 min <a href='logout.php'>Click Here to Login</a>";
            }

但是此代码不起作用,所以请帮助我如何在 session.php 页面中设置会话超时.

But this code is not working so help me how can I set session time out in session.php page.

谢谢..

推荐答案

你能试试这个吗,因为,在你的代码中你设置了每个 & 的过期时间每次页面加载时,现在添加条件.

Can you try this, Because of, in your code you have setting expire time each & every time the page gets load, now added condition.

    @session_start();       
    if(!isset($_SESSION['uname']) || $_SESSION['uname'] == " " || !isset($_SESSION['uid']) || $_SESSION['uid'] == " ")
    {
        echo "<script language='javascript'>";
        echo "window.location='../index.php'";
        echo "</script> ";
        exit;
    }

    $_SESSION['start'] = time(); // taking now logged in time

    if(!isset($_SESSION['expire'])){
        $_SESSION['expire'] = $_SESSION['start'] + (1* 10) ; // ending a session in 30 seconds
    }
    $now = time(); // checking the time now when home page starts

    if($now > $_SESSION['expire'])
    {
        session_destroy();
        echo "Your session has expire !  <a href='logout.php'>Click Here to Login</a>";
    }
    else
    {
        echo "This should be expired in 1 min <a href='logout.php'>Click Here to Login</a>";
    }

这篇关于如何在 PHP 中设置会话超时代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆