Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
<?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:
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 and b.php:
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>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900