2012-09-21 74 views
1

可能重复:
Headers already sent by PHPPHP的重定向()

我想重定向到一些页所以我尝试这样。

www.example.com/logout.php?redirect=www.index-page.com/index.php 
     echo $redirect = "'Location: http://".$_GET['redirect']."'"; 
     //redirects to index 
     header($redirect); 

但它不适用于我。是否有任何建议。

+0

这首先需要进行基本的调试,请记录(和/或显示)错误并查看PHP错误日志。此外,您将在重复问题中了解更多信息。 – hakre

回答

4

事情是这样的:

在退出链接:

<a href="http://www.example.com/logout.php?redirect=<?=urlencode('http://www.index-page.com/index.php')?>"> Logout </a> 

在logout.php页:

<? 
    // destroy session 
    header('location:'.urldecode($_GET['redirect'])); 
    die(); 
?> 
1

你不应该需要额外的'

$redirect = "Location: http://".$_GET['redirect']; 
2

不能使用echo头前,如果没有它会产生Warning: Cannot modify header information - headers already sent by

$redirect = "Location: http://". $_GET['redirect']; 
    header($redirect); 
0

您只能使用redirectecho

header("Location: http://".$_GET['redirect']); 
0

的问题是在这里

echo $redirect = "'Location: http://".$_GET['redirect']."'"; 
//redirects to index 
header($redirect); 

你不应该有头前使用的回声,这将导致警告header already sent

使用方法如下

$redirect = "Location: http://".$_GET['redirect']; 
//redirects to index 
header($redirect); 
1

下面是用户重定向到谷歌

$link='http://Google.com'; 
header("location: $link"); 

你可以把它的功能

function redirectUser($link){ 
    header("location: $link"); 
} 

一个例子,那么你可以把它叫做像

redirectUser('http://google.com'); 

或者

echo redirectUser('http://google.com'); 

没有错误会发生!

我建议你使用die();重定向代​​码后,中止代码正在添加