2012-09-21 64 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();重定向代​​碼後,中止代碼正在添加