单击注销按钮刷新 php 中的页面

我创建了一个页面作为 index.php 并添加了登录代码。它对我来说工作正常,但是当我点击注销按钮时,它正在刷新页面,如果我直接输入 URL 就像localhost/sample/testing.php它打开一样,如果我也没有登录。用户在登录之前无法访问任何页面。这是我编写的代码。我用静态数据登录,因为没有数据库。


索引.php


<?php

 session_start();

 $userinfo = array(

            'user1'=>'password1',

            'user2'=>'password2'

            );

if(isset($_GET['logout'])) {

  $_SESSION['username'] = '';

  header('Location:  ' . $_SERVER['PHP_SELF']);

}

if(isset($_POST['username'])) {

  if($userinfo[$_POST['username']] == $_POST['password']) {

      $_SESSION['username'] = $_POST['username'];

      header("Location:  dashboard.php");

  }else {

     header("Location:  index.php");

  }

}

?>

边栏.php


<?php if($_SESSION['username']): ?>

<ul>

    <li class="dropdown profile_details_drop">

        <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">

            <div class="profile_img">

                <div class="user-name">

                    <p><a href="?logout=1">Logout</p>

                </div>

                <div class="clearfix"></div>

            </div>

        </a>

    </li>

</ul>

<?php endif; ?>

如果任何用户未登录,那么他们也可以看到内页。他们在登录之前无法看到该页面。


Qyouu
浏览 118回答 3
3回答

HUX布斯

您已设置$_SERVER['PHP_SELF']. 这就是它重定向到同一页面的原因。你需要改变它,例如:login.phpif(isset($_GET['logout'])) {&nbsp; unset($_SESSION['username']);// do not set it as empty, unset it&nbsp; //header('Location:&nbsp; ' . $_SERVER['PHP_SELF']);//change this line to&nbsp;header('Location:&nbsp; login.php');}另一个错误是在您将它重定向到的 else 条件中,index.php这就是未登录用户能够看到索引页面的原因。else {&nbsp; //header("Location:&nbsp; index.php");// change this to&nbsp; header('Location:&nbsp; login.php');}注意:我只添加login.php了例如。将未登录的用户重定向到您想要的位置。

慕斯709654

首先,注销时销毁会话。并将其重定向到登录页面。假设 index.php 是登录页面。&nbsp;if(isset($_GET['logout'])) {&nbsp; &nbsp; &nbsp;session_start();&nbsp; &nbsp; &nbsp;session_destroy();&nbsp; &nbsp; header('Location: index.php');&nbsp;}在 sidebar.php 中,检查会话是否设置。如果未设置会话,则表示用户未登录。您可以通过将他们重定向到登录页面来阻止他们访问该页面&nbsp;<?php&nbsp;session_start();&nbsp;if (!isset($_SESSION["username"])){&nbsp;&nbsp;&nbsp; &nbsp; header("location: index.php");&nbsp;} ?><ul>&nbsp; &nbsp;<li class="dropdown profile_details_drop">&nbsp; &nbsp; &nbsp; <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="profile_img">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="user-name">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p>&nbsp; &nbsp; &nbsp; <a href="?logout=1">Logout</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; <div class="clearfix"></div>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; </div>&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;</li></ul>

萧十郎

首先,你的代码应该被我美化。其次,您忘记关闭a href标签,因此您的$_GET陈述 isset 不是真的。因此,通过单击该链接,该页面将再次检查if(isset($_POST['username']))哪个是真的,并且您将被重定向到您的标题的原因。考虑制定的logout.php,你使用session_destroy和session_unset你重定向你的用户login.php,例如:登出.php:<?phpsession_start();session_unset($_SESSION['username']);session_unset();session_destroy();header('Location: login.php');?>最后,考虑不使用$_GET,而偏爱$_POST或$_SESSION变量,只是因为在 URL 上不可见的原因。
打开App,查看更多内容
随时随地看视频慕课网APP