Yii重定向在服务器中无法正常工作

时间:2013-09-09 06:06:55

标签: php redirect yii

登录后,我尝试将用户根据用户角色重定向到指定页面。我尝试了重定向和标题,但两者都不适合我在服务器中。

我试过了,

$this->redirect('dashboard/profile');  
$this->redirect(array('dashboard/profile'));  
header('Location: dashboard/profile');

我收到此错误

Cannot modify header information - headers already sent by (output started at /home/deheal/public_html/dev/index.php:2) 

这一切都在本地工作,但不在服务器上请帮助我。

提前致谢。

4 个答案:

答案 0 :(得分:0)

/

之前使用URL
$this->redirect('/dashboard/profile');  

将文本输出到浏览器后,您无法使用header()。由于您的header.php可能包含输出HTML,因此无法使用header()。

您可以通过以下几种方式解决此问题:

if statement移到标题包含上方(这不起作用,正如您在header.php设置uid session和其他vital stuff的评论中所指出的那样。 调用脚本顶部的ob_start()来缓冲输出。

根据您更新的问题,您可以使用window.location代替header("location:URL")

<?php 
//Php code here
?>
      <script type="text/javascript">
       window.location= 'dashboard/profile';
        </script>
<?php 
//Php code here
?>

答案 1 :(得分:0)

如果要在页面中间的某处使用重定向,则需要确保在重定向之前没有输出到浏览器。您可以使用ob_start()函数缓冲输出,然后再将其发送到浏览器,以便在执行重定向之前不会发送标题

ob_start() .....starts buffering the output
ob_flush()......flushes out the output that was buffered

答案 2 :(得分:0)

您可以通过 javascript 执行重定向,因为渲染后无法修改标题。但是这个功能可以做到这一点。只需打电话,您就会被重定向到您想要的页面。

<?php
function redirect_url($url)
{
?>
<script>
document.title="Loading";
document.getElementsByTagName("body")[0].innerHTML="Please Wait.....";
document.getElementsByTagName("body")[0].innerHTML+='<img src="../../images/loading_2.gif" style="height:20px;width:20px;"/>';
window.location.href="<?php
echo $url;
?>";
</script>
<?php
}
?>

答案 3 :(得分:0)

之前我也遇到过同样的问题。它在我的控制器上显示标题错误。

  

复制控制器中的所有代码,然后创建新文件,粘贴   你的代码并保存相同的名称。

一定有帮助。