简体   繁体   English

如何使用户保持登录状态,直到他们注销或关闭浏览器

[英]How to keep users logged in until they are logged out or close browser

Below I have 3 php scripts going in order from when user logs in, to their login details being stored and then the log out. 在下面,我有3个php脚本,从用户登录到存储登录详细信息,然后从注销开始依次进行。 Now what I am doing at the moment is that I am using $SESSION to determine which user is logged in and then using the session_gcmaxlife to add extra time so that the session does not expire for 12 hours. 现在,我现在正在使用$SESSION来确定哪个用户已登录,然后使用session_gcmaxlife添加额外的时间,以使会话不会在12小时内过期。 So that means the user can stay logged in for 12 hours for which after that amount of time it will log the user out automatically. 因此,这意味着用户可以保持登录状态12个小时,超过该时间后它将自动注销用户。 This is just a very basic why of producing a login system. 这只是生成登录系统的一个非常基本的原因。

But what I want to do is be able to keep the user logged in for unlimited amount of time until they have either clicked on the logout link or have closed down the browser. 但我想要做的是能够让用户无限量地登录,直到他们点击退出链接或关闭浏览器。 What my question is that with the minimum amount of code change as possible, how can the codes below be altered so that they keep a user logged in until they logout or close the browser? 我的问题是,尽管代码更改量最小,下面的代码如何更改,以便用户在登出或关闭浏览器之前保持登录状态?

Can this be done with minimum change of code, the reason I am showing 5 php scripts is so that I can see what changes needs to be made for each different script, so then I should be able to make changes for other scripts within an application. 这可以通过最少的代码更改完成,我之所以显示5个php脚本是为了让我可以看到需要为每个不同的脚本做出哪些更改,那么我应该能够对应用程序中的其他脚本进行更改。

Can you please show a sample code so I can see how and where to make the changes please. 您能否请出示示例代码,以便我了解如何以及在何处进行更改。

Below are the php scripts in order to show what is currently happening: 下面是php脚本,以显示当前正在发生的事情:

  1. teacherlogin.php (This is the script where the user enters in their loggin details to log into the application) Teacherlogin.php (这是用户在其中输入登录名详细信息以登录到应用程序的脚本)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php

// connect to the database
include('connect.php');
include('member.php');

  /* check connection */
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    die();
  }

  // required variables (make them explciit no need for foreach loop)
  $teacherusername = (isset($_POST['teacherusername'])) ? $_POST['teacherusername'] : '';
  $teacherpassword = (isset($_POST['teacherpassword'])) ? $_POST['teacherpassword'] : '';
  $loggedIn = false;
  $active = true;

  if ((isset($username)) && (isset($userid))){
      echo "You are already Logged In: <b>{$_SESSION['teacherforename']} {$_SESSION['teachersurname']}</b> | <a href='./menu.php'>Go to Menu</a> | <a href='./teacherlogout.php'>Logout</a>";
  }
  else{

  if (isset($_POST['submit'])) {

      $teacherpassword = md5(md5("g3f".$teacherpassword."rt4"));  

    // don't use $mysqli->prepare here
    $query = "SELECT TeacherId, TeacherForename, TeacherSurname, TeacherUsername, TeacherPassword, Active FROM Teacher WHERE TeacherUsername = ? AND TeacherPassword = ? LIMIT 1";
    // prepare query
    $stmt=$mysqli->prepare($query);
    // You only need to call bind_param once
    $stmt->bind_param("ss",$teacherusername,$teacherpassword);
    // execute query
    $stmt->execute(); 
    // get result and assign variables (prefix with db)
    $stmt->bind_result($dbTeacherId, $dbTeacherForename,$dbTeacherSurname,$dbTeacherUsername,$dbTeacherPassword, $dbActive);

    while($stmt->fetch()) {
      if ($teacherusername == $dbTeacherUsername && $teacherpassword == $dbTeacherPassword) {
if ($dbActive == 0) {
    $loggedIn = false;
    $active = false;
    echo "You Must Activate Your Account from Email to Login";
}else {
    $loggedIn = true;
    $active = true;
      $_SESSION['teacherid'] = $dbTeacherId;
      $_SESSION['teacherusername'] = $dbTeacherUsername;
}
      }
    }

    if ($loggedIn == true){
      $_SESSION['teacherforename'] = $dbTeacherForename;
      $_SESSION['teachersurname'] = $dbTeacherSurname;
      header( 'Location: menu.php' ) ;
      die();
    }

    if (!$loggedIn && $active && isset($_POST)) {
    echo "<span style='color: red'>The Username or Password that you Entered is not Valid. Try Entering it Again</span>";
    }

       /* close statement */
    $stmt->close();

    /* close connection */
    $mysqli->close();
  }
?>

2. member.php (This script contains $SESSION variables to determine which user is logged in. This is a very important script and is included (using `include(member.php) to be able to determine if a user is already logged in or not) 2. member.php (此脚本包含$SESSION变量以确定登录的用户。这是一个非常重要的脚本并包含在内(使用`include(member.php)来确定用户是否已登录或不)

<?php

if (isset($_SESSION['teacherforename'])) {

$_SESSION['teacherforename'] = $_SESSION['teacherforename'];

}

if (isset($_SESSION['teachersurname'])) {

$_SESSION['teachersurname'] = $_SESSION['teachersurname'];

}

if (isset($_SESSION['teacherid'])) {

      $userid = $_SESSION['teacherid'];

  }

if (isset($_SESSION['teacherusername'])) {

      $username = $_SESSION['teacherusername'];

  }

        ?>

3 teacherlogout.php (Finally this is the logout page, when the user clicks on a logout link (which is only displayed in menu.php at moment) then it will go to this page where it displays a message and performs the log out by destroying the session) 3 teacherlogout.php (最后这是注销页面,当用户点击注销链接(此时仅显示在menu.php中)时,它将转到此页面,在该页面中显示消息并执行注销摧毁会议)

<?php

ini_set('session.gc_maxlifetime',12*60*60);
ini_set('session.gc_divisor', '1');
ini_set('session.gc_probability', '1');
ini_set('session.cookie_lifetime', '0');
require_once 'init.php'; 

ini_set('display_errors',1); 
error_reporting(E_ALL);


session_start();

?>

</head>

<?php

include('member.php');

?>

<body>

<?php

if ((isset($username)) && (isset($userid))){
session_destroy();
echo "You have been Logged Out | <a href='./home.php'>Home</a>";
}

else {

echo "You are Not Logged In";

}

?>

</body>
</html>

Not to be rude, but it doesn't look like you understand how sessions and cookies work correctly. 不要太粗鲁,但看起来你并不理解会话和cookie如何正常工作。 Instead of pasting 5 pages of code that no one will look at, why not try to solve the issue yourself and learn something by researching sessions + cookies? 而不是粘贴5页代码,没有人会看,为什么不尝试自己解决问题,通过研究会话+ cookie来学习一些东西? If someone gives you the answers, you will learn nothing if you do not understand the concepts behind it. 如果有人给你答案,如果你不理解它背后的概念,你就什么都学不到。

http://us3.php.net/manual/en/session.idpassing.php http://us3.php.net/manual/en/session.idpassing.php

http://us3.php.net/cookies http://us3.php.net/cookies

http://www.tuxradar.com/practicalphp/10/0/0 http://www.tuxradar.com/practicalphp/10/0/0

Also @see destroy session on window close? 还@see 窗口关闭时销毁会话吗?

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

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