php PHP标头重定向不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1242033/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:41:25  来源:igfitidea点击:

PHP header redirect not working

phpredirect

提问by JasonDavis

I know this has been covered before but I cannot find an answer to this,

我知道这之前已经涵盖过,但我找不到答案,

I have always used this;

我一直用这个;

header("Location: http://www.website.com/");
exit();

This has always worked in my current project and all of a sudden it is not working in any of my browsers

这在我当前的项目中一直有效,突然之间它在我的任何浏览器中都不起作用

I would like to figure out the problem and fix it instead of using

我想找出问题并修复它而不是使用

echo "<script type='text/javascript'>window.top.location='http://website.com/';</script>";

I also have error reporting enabled and it shows no errors

我也启用了错误报告,它没有显示任何错误

// SET ERROR REPORTING
error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);
ini_set('display_errors', TRUE);

Any ideas why it will not work?

任何想法为什么它不起作用?

回答by Mike B

Try:

尝试:

error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);


flush();
header("Location: http://www.website.com/");
die('should have redirected by now');

See what you get. You shouldn't use ^ (xor) in your error_reporting() call because you're unintentionally asking for all errors EXCEPT notices and warnings.. which is what a 'headers already sent' error is.

看看你得到了什么。你不应该在你的 error_reporting() 调用中使用 ^ (xor) 因为你无意中要求所有错误,除了通知和警告......这就是“标题已经发送”错误。

Edit:

编辑:

Also try putting flush() right above your header() call.

还可以尝试将 flush() 放在 header() 调用的正上方。

回答by Tyler Carter

Try removing the Space Between location and the first h in http.

尝试删除位置之间的空间和 http 中的第一个 h。

header("Location: http://www.website.com/");
exit();

turns into

变成

header("Location:http://www.website.com/");
exit();

I had this problem on my WAMP Server.

我在 WAMP 服务器上遇到了这个问题。

Although it shouldn't be the problem, considering that is how it is documented in the PHP documentation. But you should probably try it anyway. I know it has worked for me in a number of cases.

尽管这应该不是问题,但考虑到 PHP 文档中是这样记录的。但无论如何你应该尝试一下。我知道它在许多情况下对我有用。

回答by T.Todua

COMMON PROBLEMS:

常见问题:

1)there should not be any output (i.e. echo..or HTML codes) before the header(.......);command.

1)echo..header(.......);命令之前不应该有任何输出(即或 HTML 代码)。

2)there should not be a white-space(or newline) before <?phpand after ?>tags.

2)不应该有一个空白(或换行之前)<?php和后?>标记。

3)GOLDER RULE!- the file (and other include()-d files) should have UTF8 without BOMencoding (and not just UTF-8). That is problem in many cases (because typical UTF8encoded file has something special character in the start of file output)!!!!!!!!!!!

3)黄金法则!- 文件(和其他include()-d 文件)应该有没有 BOM编码的UTF8(而不仅仅是UTF-8)。这在很多情况下都是问题(因为典型的UTF8编码文件在文件输出的开头有一些特殊字符)!!!!!!!!!!!!

4)When redirecting, after header(...);you must use exit;

4)重定向后,header(...);您必须使用exit;

5)Recommended practice - always use 301 or 302 in reference:

5)推荐做法 - 始终使用 301 或 302 作为参考:

header("location: http://example.com",  true,  301 );  exit;

6)If none of above helps, use JAVSCRIPT redirection(but it's highly not recommended By Google), but it may be the last chance...:

6)如果以上都没有帮助,请使用 JAVSCRIPT 重定向(但 Google 强烈不推荐这样做),但这可能是最后的机会......:

echo "<script type='text/javascript'>window.top.location='http://example.com/';</script>"; exit;

回答by karto

try this. worked for me.

尝试这个。为我工作。

echo "<meta http-equiv='refresh' content='0;url=http://www.yoursite.com'>";

echo "<meta http-equiv='refresh' content='0;url=http://www.yoursite.com'>";

回答by Mahmoud Salem

It may be strange solution but try this, change the page encoding from utf8 to ANSI and it will work.

这可能是奇怪的解决方案,但试试这个,将页面编码从 utf8 更改为 ANSI,它会起作用。

use any text editor and save the page as ANSI encoding and upload it to your online server.

使用任何文本编辑器并将页面保存为 ANSI 编码并将其上传到您的在线服务器。

回答by Toby Allen

Also when you are using the header function it has to be the first thing called before any text (even a space) is written to the client, so check again that there is no spaces being output prior to your call even before th

此外,当您使用标头函数时,它必须是在将任何文本(甚至是空格)写入客户端之前调用的第一件事,因此请再次检查在调用之前甚至在调用之前没有输出空格

<?php

回答by Gayathri

Adding ob_start() solved this issue.

添加 ob_start() 解决了这个问题。

回答by Umair Sultan

Try this (working for me):

试试这个(为我工作):

echo '<script>window.location.replace("http://www.example.com")</script>';

echo '<script>window.location.replace("http://www.example.com")</script>';

Instead of

代替

header("Location: http://www.example.com");

header("Location: http://www.example.com");

回答by Emre AYDIN

If your index page is a html file, it may not work. Change it to index.php and use this code:

如果您的索引页是一个 html 文件,它可能无法工作。将其更改为 index.php 并使用以下代码:

<?php

header("Location: http://ea.tc");

?>

回答by Matt Croak

I actually had a case similar to this where I had an admin page that was included at the top of all my other pages. At the top of each page below the line:

我实际上有一个与此类似的案例,其中我有一个管理页面,该页面包含在我所有其他页面的顶部。在每页顶部的行下方:

<?php include '../../admin.php' ?>

I would have the php logic:

我会有php逻辑:

<?php if($_SESSION['username'] === null){ header("Location: ./adminLogin.php");}?>

The problem with this was that somewhere else I was also calling/manipulating the header(.... After a lot of time going through my code I admit I could not figure out where the problem was. Then I thought that each of these files hits my admin.phpfile before doing anything else. So I thought about what would happen if I would put the logic that was at the top of each of my views (because I didn't want anything to be visible unless you were logged in) into my admin.phpfile?

这样做的问题是在其他地方我也在调用/操作header(.... 经过大量时间浏览我的代码后,我承认我无法弄清楚问题出在哪里。然后我认为这些文件中的每一个admin.php在做其他事情之前都会命中我的文件。所以我想如果我将位于每个视图顶部的逻辑(因为我不希望任何内容可见,除非您登录)到我的admin.php文件中会发生什么?

What happened was that before it even got to any of the php/html in my views it evaluated whether or not someone was logged in ($_SESSION['username'])) and if it was NULLthen I just redirected to the adminLogin page. I put this logic right before my switch and it's worked perfectly for all my files that once required the logic. The way I had it worked in development, but posed a lot of issues in production. I found that moving the redirection logic to my admin.phpfile not only avoided the duplicate header(...manipulation but actually made my code more efficient by removing the excess logic from my view files and into my admin.phpfile.

发生的事情是,在它到达我视图中的任何 php/html 之前,它会评估是否有人登录 ( $_SESSION['username'])),如果是,NULL则我只是重定向到 adminLogin 页面。我把这个逻辑放在我的开关之前,它对我曾经需要逻辑的所有文件都非常有效。我在开发中使用它的方式,但在生产中提出了很多问题。我发现将重定向逻辑移到我的admin.php文件中不仅避免了重复header(...操作,而且通过从我的视图文件和我的admin.php文件中删除多余的逻辑实际上使我的代码更高效。

Rather than putting the logic in every view file, put it in your controller once, before your switch. Works like a charm! This is useful if you don't want anyone to access any of the sensitive views unless they log in. In my case this was important for my CMS. However, if there are some files that you want to be viewable without logging in then I think the original logic would be sufficient. It seems like you already found a solution but hopefully this can be helpful if you run into this error again. :)

与其将逻辑放在每个视图文件中,不如在切换之前将其放入控制器一次。奇迹般有效!如果您不希望任何人访问任何敏感视图,除非他们登录,这很有用。在我的情况下,这对我的 CMS 很重要。但是,如果您希望在不登录的情况下查看某些文件,那么我认为原始逻辑就足够了。您似乎已经找到了解决方案,但希望如果您再次遇到此错误,这会有所帮助。:)