简体   繁体   English

如何使用户使用PHP登录一个月?

[英]How to keep user logged in for one month in PHP?

I have searched all through StackOverflow and other forums, and have not found a way to make my code work. 我已经通过StackOverflow和其他论坛进行了全部搜索,但是还没有找到使我的代码正常工作的方法。 I have a basic login mechanism setup for my users, and it works just fine. 我为我的用户设置了基本的登录机制,并且工作正常。 However, when a browser is closed the user must visit the site and log back in. I need to find a way to keep the user logged in for a period of one month. 但是,关闭浏览器后,用户必须访问该站点并重新登录。我需要找到一种方法来使用户保持登录状态一个月。

I researched cookies and experimented for two days, but consistently when I exit out of the browser and load the site back up, the user is logged out. 我研究了cookie并进行了为期两天的实验,但是当我退出浏览器并加载网站备份时,用户始终注销。 The only thing that sort of worked was using ini_set to change the session.gc_maxlifetime and session.lifetime variables, but for some odd reason no matter what values I assigned to those two variables the user would only stay logged in for around 4 hours. 唯一有效的方法是使用ini_set来更改session.gc_maxlifetime和session.lifetime变量,但是出于某种奇怪的原因,无论我为这两个变量分配了什么值,用户都只能保持登录状态约4个小时。 So, does anyone know if I am doing something wrong in setting the cookie below, or how I can fix my problem? 那么,有人知道我在设置下面的cookie时是否做错了什么,或者如何解决我的问题? Thank you in advance for your answers, I am at my wits end with this issue. 预先感谢您的回答,我不知所措。

        //create cookie, set expiration to one month (in seconds)

        setcookie("user",$username,time()+2592000);
        echo $_COOKIE["user"];

        session_start();

        //Login Successful, begin session

        $member = mysqli_fetch_assoc($result);
        $_SESSION['SESS_MEMBER_ID'] = $member['id'];
        $_SESSION['SESS_FIRST_NAME'] = $member['username'];
        $_SESSION['SESS_LAST_NAME'] = $member['password'];
        $_SESSION['table'] = $table;

        session_write_close();

        //header("location: search.php");
        exit();

In loggin page add (sessions): 在登录页面中添加(会话):

$_SESSION['timeout'] = time();

and index check: 和索引检查:

session_start();

$timeout = 7200; //time in seconds

if(isset($_SESSION['timeout'])) {
    $duracao = time() - (int) $_SESSION['timeout'];
    if($duracao > $timeout) {
      session_start();
      session_destroy();
      session_unset();
      echo "<script type='text/javascript'>location.href='index.php'</script>";
    }
}

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

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