php header() 在本地主机上工作但在实时服务器上不工作?
if ($num_rows == 0 && $num_rows2 == 0) {
echo 'result1';
header('Location:enter-work.php');
die();
} else if ($num_rows >= 0 || $num_rows2 >= 0) {
echo 'result2';
header('Location:similar-work.php');
die();
}
我正在测试上面的代码,每次我使用此代码刷新页面时,它都会正确回显示例文本“result1”或“result2”,但它不会重定向到 Enter-work.php 或类似的内容-work.php 就像在我的本地主机服务器上一样。在我的本地主机服务器上,我什至可以将其链接到在线网站(即 google.com)。我在编写任何 html 之前输入了上面的代码 - 所以甚至在声明 doctype 之前。
不确定是什么导致了问题,需要一些帮助。
编辑:正如 Pekka 在他的评论中提到的(关于所选答案),问题是我在 header() 函数之前有回声和空格。
if ($num_rows == 0 && $num_rows2 == 0) {
echo 'result1';
header('Location:enter-work.php');
die();
} else if ($num_rows >= 0 || $num_rows2 >= 0) {
echo 'result2';
header('Location:similar-work.php');
die();
}
I have the above code that I'm testing with and it properly echos out the sample text 'result1' or 'result2' every single time I refresh the page with this code, but it is not redirecting to enter-work.php or similar-work.php like it does on my localhost server. On my localhost server, I can even link it to online websites (ie. google.com). And I inputed the above code before any html was written - so before even the doctype was declared.
Not sure what's causing the problem, need some help.
EDIT: as Pekka mentioned in his comments (on the selected answer) the problem was I had echos and white spaces before the header() function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在发送标头之前不得输出任何内容。
如果您这样做,本地服务器和实时服务器的行为会有所不同,这可能是由于不同的输出缓冲设置所致。
无论如何,如果你消除回声,它应该会起作用。
You mustn't output any content before sending headers.
That the local and live server behave differently if you do, may be because of different output buffering settings.
Anyway, it should work if you remove the echos.
对于任何仍然面临同样问题的人:
首先在
yourfile.php
中尝试简单的header()
代码,如果问题解决了,则删除任何其他内容,这意味着旧代码中的问题。第二:
header ()
应该可以使用其他内容,例如 echo "" 等...最后:如果您尝试了简单的
header()
代码,但问题仍然没有解决请联系您的托管提供商。for any one still face same problem :
first try just simple
header()
code inyourfile.php
and remove any other content if the problem solved that is mean the problem in your old code.second :
header ()
should work even with another content like echo "" etc ...last :if you tried simple
header()
code and the the problem still not solved contact your hosting provider.