单击浏览器后退按钮后缺少会话数据

单击浏览器后退按钮后缺少会话数据

问题描述:

我正在用php开发一个简单的成员管理系统,但遇到了一个问题:

用户登录并将其重定向到主页,并将用户ID保存在会话中;在用户单击并尝试通过按浏览器的后退"按钮返回主页面后,主页上还有一些指向其他页面的链接,有时会话中的用户ID丢失.

我已经检查了会话保存路径,单击上一步"按钮时会创建一个新的会话文件,因此我假设session_start()为此创建了一个新会话;但我仍然不知道为什么,这是一个随机的情况...

有什么办法解决吗?

main.php:

I''m developing a simple member management system with php, and I''ve met a problem:

The user logs in and it is redirected to a main page and the user ID is saved in the session; there are some links to other pages in the main page, after the user clicks and is trying to go back to main by pressing browser "Back" button, sometimes the user ID in the session is lost.

I''ve checked the session save path, a new session file is created when I click "Back" button, so I assume the session_start() creates a new session for it; but I still don''t know why, it''s a random case...

Is there any way to solve it?

main.php:

<?php session_start(); ?>
<?php
$echo_string = '
<body>
  <a href="a.php">a</a>
  <a href="b.php">b</a>
</body>';

if (!empty($_SESSION['user']))
  echo $echo_string;
else
  header("Location: login.php");
?>


login.php:


login.php:

<?php
  session_start();
  if (isset($_POST['userLogin'])) {
    $_SESSION['user'] = $_POST['userLogin'];
    // check userLogin in db
    ...
  }
  header("Location: main.php");
?>
<form novalidate="" method="post" action="login.php">
  <label class="hidden-label" for="Username">Username</label>
  <input id="Username" name="userLogin" type="text" placeholder="Username" value="" spellcheck="false" class="">
  <label class="hidden-label" for="Passwd">Password</label>
  <input id="Passwd" name="userPassword" type="password" placeholder="Password" class="">
  <input id="signIn" name="signIn" class="rc-button rc-button-submit" type="submit" value="Log in">
</form>


a.php和b.php:


a.php and b.php:

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
  <head>...</head>
  <?php
    $echo_string = '...'; // a html format string
    if (!empty($_SESSION['user']))
      echo $echo_string;
    else
      header("Location: login.php");
  ?>
</html>

echo_string = ' <身体> < a href ="a.php"> a</a> < a href ="b.php"> b</a> </body>'; 如果(!empty(
echo_string = ' <body> <a href="a.php">a</a> <a href="b.php">b</a> </body>'; if (!empty(


_SESSION [' 用户'])) echo
_SESSION['user'])) echo


echo_string ; 其他 标头(" ); ?>
echo_string; else header("Location: login.php"); ?>


login.php:


login.php:

<?php
  session_start();
  if (isset(