2015-11-02 289 views
-4

下面的代码显示我“111”和“222”。但不会将我重定向到任何地方。我现在能做什么?重定向和标题不起作用

<?php 
echo "111"; 
header("Location: http://www.google.com"); 
echo "222"; 
return Redirect::to('http://www.google.com'); 
echo "333"; 
?> 
+5

http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php检查此链接 –

+0

可能重复[如何解决“头已发送”错误PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Epodax

+0

使用ob_start();在你的代码的开头 – 2015-11-02 14:17:19

回答

0

你不能有标题标签设置之前任何输出,所以头后删除echo语句,并退出脚本:

header("Location: http://www.google.com"); 
exit; 
+0

谢谢,但仍然没有任何反应 – divHelper11

+0

你发布脚本的完整脚本? – Ashley

1

两件事情......

你需要把你的exit头之后( “位置:...”) 例子:

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

但更重要的是,如果您已使用echo写入输出缓冲区,您的标题将不会重定向。

+0

谢谢,但仍然没有任何反应 – divHelper11

+1

请注意,在使用header()之前,您不能有任何输出。所以没有回声,var_dump,没有或任何以上的PHP标记,没有包括标题,什么都没有。 – Ivar

-1

这不起作用,因为您已经将数据发送到客户端(echo)。要么把你的头,你什么呼应前,或缓冲这样的:

ob_start(); 
echo "111"; //Note: You're missing a semicolon here 
header("Location: http://www.google.com"); 
echo "222"; 
$obj = Redirect::to('http://www.google.com'); 
echo "333"; 
ob_flush(); 
return $obj; 

To Continue Reading... 希望这有助于!

+0

return语句将跳出这一点,ob_flush()将永远不会到达 – bashaus