Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单击注销按钮以php刷新页面_Php - Fatal编程技术网

单击注销按钮以php刷新页面

单击注销按钮以php刷新页面,php,Php,我已经创建了一个名为index.php的页面,并添加了登录代码。它对我来说工作正常,但当我点击注销按钮时,它会刷新页面,如果我直接输入URL,比如localhost/sample/testing.php,如果我没有登录,它就会打开。用户在登录之前无法访问任何页面。这是我写的代码。我使用静态数据登录,因为没有数据库 Index.php <?php session_start(); $userinfo = array( 'user1'=>'password1'

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

Index.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 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; ?>

Sidebar.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 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; ?>


如果任何用户没有登录,那么他们也可以看到内部页面。他们只有登录才能看到页面。

您已经设置了
$\u服务器['PHP\u SELF']
。这就是它重定向到同一页面的原因。您需要更改它,例如:
login.php

if(isset($_GET['logout'])) {
  unset($_SESSION['username']);// do not set it as empty, unset it
  //header('Location:  ' . $_SERVER['PHP_SELF']);//change this line to
 header('Location:  login.php');
}
另一个错误是在else条件下,您将其重定向到
index.php
,这就是为什么未登录的用户能够看到索引页面

else {
  //header("Location:  index.php");// change this to
  header('Location:  login.php');
}
注意:我添加的
login.php
仅用于例如。将未登录的用户重定向到您想要的位置


在注销时取消设置会话变量:

unset($_SESSION['username']);
而不是分配给空字符串:

$_SESSION['username'] = '';

首先,你的代码应该美化我

其次,您必须忘记关闭
a href
标记,因此您的
$\u GET
语句设置不是真的。因此,通过单击该链接,页面将再次检查
if(isset($\u POST['username'))
是否为true,并且由于标题的原因,您将被重定向

考虑制作一个
logout.php
,使用
session\u destroy
session\u unset
并将用户重定向到
login.php
,例如:

logout.php:

<?php
session_start();
session_unset($_SESSION['username']);
session_unset();
session_destroy();
header('Location: login.php');
?>


<>最后,考虑不使用<代码> $yGET,但更喜欢<代码> $POST 或<代码> $ySession 变量,仅由于URL上不可见的原因。

< P> >首先,在注销时销毁会话。并将其重定向到登录页面。假设index.php是登录页面

 if(isset($_GET['logout'])) {
     session_start();
     session_destroy();
    header('Location: index.php');
 }
在sidebar.php中,检查会话是否已设置。如果未设置会话,则表示用户未登录。您可以通过将他们重定向到登录页面来阻止他们访问该页面

 <?php
 session_start();
 if (!isset($_SESSION["username"]))
{  
    header("location: index.php");
 } ?>


我的索引页面本身,我在其中添加了登录功能代码,但是我如何将页面限制为用户,直到他们登录为止。您必须使用$\u会话变量。如果这样做,还可以实现CSRF令牌等机制来防止其他已知攻击。例如,当用户登录时,您必须使用
重新生成会话id()
,它将覆盖从登录页面获得的当前会话id。这样,只有具有此id的人才能访问您的索引文件。此外,在注销时,您可以取消设置会话并销毁它以删除此id。