php 标头('位置:..')不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3547913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
header('location: ..') not working
提问by Fortisimo
(1)I'm in the process of uploading my website to a remote web server.
(1) 我正在将我的网站上传到远程 Web 服务器。
(2)The site's template system is set up in a way that all of the pages are formed by sending url-encoded get requests to index.php
(2)网站的模板系统设置为所有页面都是通过发送url编码的get请求到 index.php
(3)Loading up the initial page works. This page determines the location of the next page by evaluating the value of its form.
(3)加载初始页面有效。此页面通过评估其表单的值来确定下一页的位置。
(4)The redirection to the next page is performed by doing a: header('location: next_page')
(4)重定向到下一页是通过执行以下操作来执行的: header('location: next_page')
(5)For some reason, the redirection is not performed. Here's what the code looks like:
(5)由于某种原因,没有执行重定向。代码如下所示:
$error = "";
if(isset($_POST['index_choice'])){
$path_choice = isset($_POST['path']) ? $_POST['path'] : NULL;
//echo $path_choice;
//echo $page_inc;
//nothing after this
if($path_choice != null){
if($form->is_connected()){
//if($path_choice != "" || $path_choice != NULL){
if($path_choice == "new"){
//header('location: /login.php');
//header('location: page/login');
header('location: /index.php?page=login');
exit();
}
else{
//header('location: /amend.php');
//header('location: page/amend');
header('location: /index.php?page=amend');
exit();
}
//}
/**
else{
//destroy_session();
$error = "You haven't selected a path. Please choose a path";
}
*
*/
}
else{
//destroy_session();
$error = "Problems with connecting to the database";
}
}else{
//destroy_session();
$error = "You have not indicated your choice";
}
}
SOLVED
解决了
It was a matter of having a blank space after a ?>
somewhere else in the code. This was revealed to me after placing the following commands at the top of the code:
这是?>
在代码中的其他地方之后有一个空格的问题。在代码顶部放置以下命令后,我发现了这一点:
error_reporting(E_ALL); ini_set('display_errors', 'On');
I'd like to say thanks to all of the people that have tried to help.
我要感谢所有试图提供帮助的人。
回答by
Looks like you're echo-ing text to the browser before sending the header('location'). You can not send content to your browser before executing a header(), as your echo will force a header to be sent. Comment these lines out and see if it works:
看起来您在发送标题('位置')之前将文本回显到浏览器。在执行 header() 之前,您不能将内容发送到浏览器,因为您的回声会强制发送标头。注释掉这些行,看看它是否有效:
// echo $path_choice;
// echo $page_inc;
Now your header will be sent and you will be redirected.
现在您的标头将被发送,您将被重定向。
回答by Erfan Safarpoor
addthis code to firstof code:
将此代码添加到代码的第一个:
<?php
ob_start();
?>
回答by shasi kanth
You can try the javascript way of redirecting the page:
您可以尝试重定向页面的 javascript 方式:
echo '<script>window.location = "'.$url.'";</script>';
回答by Hariharaselvam Balasubramanian
Just try this. it will work
试试这个。它会工作
echo "<script type='text/javascript'> window.location='index.php'; </script>";
instead of header("location: index.php");
代替 header("location: index.php");
回答by joschi
Does simple PHP script like the following work?
像下面这样的简单 PHP 脚本是否有效?
<?php header('Location: /index.php?page=login'); ?>
You also have to make sure that the code path with $path_choice != null
and $form->is_connected() === true
is actually taken because your error states don't set HTTP headers at all.
您还必须确保实际采用了带有$path_choice != null
和的代码路径,$form->is_connected() === true
因为您的错误状态根本没有设置 HTTP 标头。
回答by Lekensteyn
I see a 'destroy_session()', are you using session_start() somewhere? Make sure that NO content (including (session)cookies) is already sent.
我看到一个 'destroy_session()',你在某处使用 session_start() 吗?确保没有内容(包括(会话)cookies)已经发送。
Tips for debugging: set error_reporting(E_ALL) (including E_NOTICE). That'll give you the line on which the headers are sent.
调试提示:设置error_reporting(E_ALL)(包括E_NOTICE)。这将为您提供发送标头的行。
Other sources of trouble: - BOM- extra lines before in an included file
其他问题来源: - BOM- 包含文件中的额外行
回答by Nerudo
Make sure before <?php
of the php file you dont have a space. It might help if you changed the file encoding type to UTS_8 without BOM and backspace just before <?php
and make sure you dont have session start()
after anything in the file. It should be just after
确保在<?php
php 文件之前没有空格。如果您将文件编码类型更改为 UTS_8 之前没有 BOM 和退格,<?php
并确保session start()
文件中没有任何内容,这可能会有所帮助。应该就在之后
回答by Nucleon
I believe you need to check your if-stack and make sure you are getting into each of your loops. Place an echo after each if statement to make sure the header() is even being evaluated, your loop is prolly being cut earlier than you expect.
我相信您需要检查您的 if-stack 并确保您进入每个循环。在每个 if 语句之后放置一个 echo 以确保 header() 甚至正在被评估,你的循环比你预期的更早被切断。
My suspect has to do with the usage of null and $path_choice != null. Have you tried is_null() or isset() since they were designed specifically to check if a variable is null. Also, the !== operand may work in this case too, but i'm not sure offhand if PHP has that.
我的怀疑与 null 和 $path_choice != null 的使用有关。您是否尝试过 is_null() 或 isset() ,因为它们专门用于检查变量是否为空。此外, !== 操作数也可以在这种情况下工作,但我不确定 PHP 是否有。
回答by phpapprentice
Just make sure before your php code you do not have a space because this does matter.
请确保在您的 php 代码之前没有空格,因为这很重要。
For example
例如
<?php
some code this is the correct way
?>
--------------
<?php
some code this is the incorrect way the space above matters
?>
回答by Clayton Kappaz
Well, if you tried everything, this example can help (HTML/PHP):
好吧,如果你尝试了一切,这个例子可以帮助(HTML/PHP):
<?php
header("HTTP/1.0 404 Not Found");
echo '<html>
<head>
<meta http-equiv="Refresh" content="0;url=http://www.url.com/index.php?code=404" />
</head><body></body>
</html>';
?>