我有一个logout.php
文件位于根目录中,它像这样
<?php
if(isset($_POST['logout'])){
session_destroy();
header('location: ../index.php');
}
?>
<form method="POST">
<button name="logout" class="logout">Logout</button>
</form>
此按钮包含在header.php
中,而header.php
实际上是导致问题的原因,该问题如下:
有两个头文件,分别位于两个不同的目录中,一个位于根文件夹中,另一个位于子文件夹/users/
中,如果您已看到代码,则可能已经假定注销按钮已执行每次您位于/users/
目录中时,它便正常工作,例如。 localhost/rootdirectory/users/dashboard.php
,但是每次您访问根文件夹并尝试从那里注销时,它会将您重定向到一个目录,从而直接进入WAMP的索引页面(在我的情况下)。
现在您必须怀疑为什么我要使用两个不同的标题,所以这里是对此的解释:
header.php
中的内容在两个文件中都是完全相同的,但是我有一个重复的内容,因为我发现它是我创建时遇到的相同类型问题的唯一解决方案header.php
或更准确地说就是在创建子文件夹/users/
并尝试将/rootfolder/header.php
包含在/rootfolder/users/index.php
中之后。
这里发生的事情类似于我的注销按钮现在发生的事情,而我之所以问这个问题的主要原因是因为每次遇到此类问题我都无法创建重复项。
当我尝试将/rootfolder/header.php
包含在/rootfolder/users/index.php
中时,我失去了与链接到包含style.css
的标头内的每个文件的连接
现在我们位于/rootfolder/users/index.php
<?php
include '../header.php';
include '../footer.php';
?>
这就是/rootfolder/header.php
的样子
<?php
session_start();
//we link the connection file
include "pagesetup/config.php";
//we populate the database with tables and default data
include "pagesetup/createtables.php";
//we store the page title inside of a variable
$get_title = mysqli_query($mysqli, "SELECT title FROM pageinfo");
while($row = $get_title->fetch_assoc()){
$page_title = $row['title'];
}
//we unset the login and register sessions which are used to display the outcome
//of the login/register attempt
unset($_SESSION['login']);
unset($_SESSION['register']);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="js/javascript.js"></script>
<title>
<?php echo $page_title;?>
</title>
</head...
在这种情况下,我的索引页看起来像这样,并且我无法访问其中链接的任何其他文件(包括最重要的config.php
)
将style.css
文件应用于/rootfolder/users/index.php
的唯一方法是在header.php
内复制/rootfolder/users/
,然后像这样更改其路径
<?php
session_start();
//log in protection
if(!isset($_SESSION['loggedin'])){
header('location:../index.php');
}
//we link the connection file
include "../pagesetup/config.php";
//we populate the database with tables and default data
include "../pagesetup/createtables.php";
//we store the page title inside of a variable
$get_title = mysqli_query($mysqli, "SELECT title FROM pageinfo");
while($row = $get_title->fetch_assoc()){
$page_title = $row['title'];
}
//we unset the login and register sessions which are used to display the outcome
//of the login/register attempt
unset($_SESSION['login']);
unset($_SESSION['register']);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../css/style.css" />
<script type="text/javascript" src="../js/javascript.js"></script>
<title>
<?php echo $page_title;?>
</title>
</head...
然后当然也要更改/rootfolder/users/index.php
中的路径
<?php
include 'header.php';
include '../footer.php';
?>
这个问题可能与主题无关,但是我找不到正确的方法提出问题,因此,如果您了解我的问题,并且有可以帮助我学习使用目录的内容,我会很高兴听到它。
和平!
答案 0 :(得分:0)
让我们简化一下:
检查会话是否存在,如果存在则将其删除,然后重定向到另一个页面。
<?php
session_start();
session_destroy();
exit(header("Location: ../index.php"));
?>
将其另存为单独的脚本“ logout.php”,而不是使用按钮,只需从任何/所有适当的页面链接到它<a href="logout.php">logout</a>
即可。除非您真的致力于使用按钮...
附加:
logout.php
文件包含<form method="POST">
:
没有action="logout.php"
会将您发送到该文件(单独的文件)。的
您显示的其余代码是<head></head>
区域(两者均
页),而不显示按钮表单的HTML。logoff.php
文件。logoff.php
的输出
前往2个不同的位置。如果您将两者都发送到同一文件,
那么您只需要担心您的logoff.php
脚本具有正确的Location: ../index
路径即可。如果index.php
和logoff.php
在
相同级别的目录,则正确的重定向路径将是:
Location: index.php
。总而言之,如果您的站点访问者单击注销(按钮或链接),则应将其发送到单独的logoff.php
脚本中。该脚本又将它们“重定向”(或发送)到index.php
页面。
由于这两个页面位于不同的目录中,因此您将不得不使用不同的路径将用户发送到logoff.php
脚本。示例:
localhost/rootdirectory/users/dashboard.php
的链接是一个
目录位于根目录之上,链接应为../logout.php
。logout.php
。logout.php
中的重定向链接保持不变
index.php
。