简体   繁体   English

防止用户登出后进入

[英]Prevent user from entering once logged out

So I designed a log in system.所以我设计了一个登录系统。 However when ever the user logs in I redirect him to a new page where there is a menu.但是,当用户登录时,我会将他重定向到一个有菜单的新页面。 But when ever the user logs out and copies the home page URL.但是当用户注销并复制主页 URL 时。 He is capable of coming back again.他有能力再次回来。 How do I prevent this?我如何防止这种情况?

Login.php登录.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>

Logout:登出:

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

And the menu page:和菜单页面:

 <a href="login.php">Logout</a>

Try this code and put in home page in top.试试这个代码并将主页放在顶部。

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

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

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