douduan4116 2016-09-19 08:54
浏览 28
已采纳

一旦注销,阻止用户进入

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. He is capable of coming back again. How do I prevent this?

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>

Logout:

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

And the menu page:

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

</div>
  • 写回答

1条回答 默认 最新

  • doufuxing8691 2016-09-19 08:58
    关注

    Try this code and put in home page in top.

    if(!isset($_SESSION['username'])) {
       header("Location: login.php");
       exit;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器