标题('位置:')不起作用

问题描述 投票:0回答:4

那么,有两个问题!

  1. 使用 header('location: newHome.php') 时该文件是否需要位于同一目录中?

    我将把我的signOut.php放在主目录中,并且能够从任何页面退出网站.. example.com/example/example.example.html

  2. 我的代码只是在 example.com/signOut.php 返回一个错误页面。signOut.php 与我测试它的目录位于同一目录中..

example.com/example.php

<li><a href="signOut.php"><strong>Sign Out</strong>

example.com/signOut.php

<?php 
session_start();
$_SESSION = array();
session_destroy();
header('Location: http://www.example.com/newHome.html');
?>
php
4个回答
0
投票

1)回答您的第一个问题(当使用 header('location: newHome.php') 时,该文件是否需要位于同一目录中?)

如果您不提供任何路径,那么它必须位于同一目录中。

但是,您需要为网站 URL 定义常量,如下所示:

define(SITE_URL,"http://www.example.com/");

然后在您网站的任何位置使用它,如下所示:

<li><a href="<?php echo SITE_URL; ?>signOut.php"><strong>Sign Out</strong>

点击以下链接了解有关常量的更多信息:

http://php.net/manual/en/function.constant.php

2)回答你的第二个问题(我的代码只是在 example.com/signOut.php 返回一个错误页面)

发生这种情况是因为您的路径不正确。手动更正路径或按照我的第一个答案并定义常量来更正它。

我认为 headers 没有任何问题,就像您所说的 404 Not found 错误一样。


0
投票

都可以在同一个文件夹中

header('location: newHome.php');

或者,不在同一个文件夹中

header('location: path/to/newHome.php');

或者完整的 URL

header('Location: http://www.example.com/newHome.html');

0
投票

创建一个名为 logout.php 的文件,其中包含:

<?php
// Unset all of the session variables.
session_start(); 
session_destroy();
//on a ubuntu 20.04 server Location doesn't redirect!!!
// location does! so USE the l and not a L in the code
header("location: https://".$domeinnaam."/u_bent_uitgelogd.php");
exit();
?>

-1
投票
ob_start();
header('Location: http://www.example.com/newHome.html');

只需在标题之前使用 ob_start(); 会有帮助

© www.soinside.com 2019 - 2024. All rights reserved.