一旦注销,阻止用户进入

时间:2016-09-19 08:54:22

标签: php session

所以我设计了一个登录系统。但是,当用户登录时,我将其重定向到有新菜单的新页面。但是,当用户注销并复制主页URL时。他有能力再次回来。我该如何防止这种情况?

的login.php



<html>

<head>
  <title>Log in</title>
</head>

<body>
  <h1>Welcome please sign in!</h1>

  <?php if (!isset($_POST[ 'submit'])){ ?>
  <!-- The HTML login form -->
  <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    Username:
    <input type="text" name="username" />
    <br />Password:
    <input type="password" name="password" />
    <br />

    <input type="submit" name="submit" value="Login" />
  </form>
  <?php } else { require_once( "db_const.php"); $mysqli=n ew mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); # check connection if ($mysqli->connect_errno) { echo "
  <p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>"; exit(); } $username = $_POST['username']; $password = $_POST['password']; $sql = "SELECT * from users WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
  $result = $mysqli->query($sql); if (!$result->num_rows == 1) { echo "
  <p>Invalid username/password combination</p>"; } else { echo '
  <script language="javascript">
    ';
		echo '
    alert("Log in successfull!")
    ';
		echo '
  </script>'; header( "refresh:5;url=home.php" ); } } ?>
</body>

</html>
&#13;
&#13;
&#13;

注销:

&#13;
&#13;
<?php
session_start();
session_destroy();
header('Location: login.php');
exit;
?>
&#13;
&#13;
&#13;

菜单页面:

&#13;
&#13;
<a href="login.php">Logout</a>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

尝试使用此代码并将主页放在首页。

if(!isset($_SESSION['username'])) {
   header("Location: login.php");
   exit;
}