2010-04-29 126 views
1

我使用php腳本進行重定向後,搜索引擎搜索到我的websiter的搜索詞。php腳本重定向代碼問題

和我的重定向代碼工作正常

但對一些物關鍵詞我笏留在同一個頁面。對於這些代碼行,我在頁面中收到一條警告消息。

Warning: headers already sent 
      (output started at /home/friendsj/public_html/index.php:2) in 
      /home/friendsj/public_html/index.php on line 20 
下面

爲i應用於的網頁

$ref=$_SERVER['HTTP_REFERER']; 

if(strstr($ref,"test")){ 
    $url="http://www.howtomark.com/robgoyette.html"; 
} 
else if(strstr($ref,"mu+word+gmail")){ 
    $url="http://www.howtomark.com/marketbetter.html"; 
} 
else{ 
    if(strstr($ref,"how+to+market+better")){ 
    }  
} 

if($url !=""){ 
    header("Location:$url"); 
} 
+0

您可以使用 '0101010' 按鈕格式化代碼。否則,它不可讀。 – 2010-04-29 09:53:08

+0

同時在「?>」後檢查文件末尾是否有空格 – 2010-04-29 10:21:58

回答

1

重定向通過設置HTTP頭實現,作爲代碼功能建議使用header()。這意味着您只能在之前重新定向,然後才能開始文檔輸出。無論您在第2行開始打印什麼,稍後執行;-)

+0

Thanksssssssssssssssssss – 2010-04-29 10:24:11

0

除去任何輸出開始的第二線在index.php的

0

如果您使用Header函數,則在調用該函數之前不允許輸出某些內容。

在你的情況下,你在第2行寫在index.php東西輸出。

一個不好的解決方法是使用輸出緩存函數ob_start。 但這不是一個真正的解決方案。

+1

爲什麼輸出緩存功能不是真正的解決方案? – 2010-04-29 09:56:15

+1

@Manos:因爲更好的是治癒疾病,而不是僅僅隱藏症狀 – zerkms 2010-04-29 10:18:24

+0

zerkms博士是在錯誤的社區,我們需要一個編碼器來解釋:) – 2010-04-29 10:27:00

0

試試這個

<?php ob_start(); 
$ref='some text goes here'; 

if(strstr($ref,"test")){ 
    $url="http://www.howtomark.com/robgoyette.html"; 
} 
else if(strstr($ref,"mu+word+gmail")){ 
    $url="http://www.howtomark.com/marketbetter.html"; 
} 
else{ 
    if(strstr($ref,"how+to+market+better")){ 
    }  
} 

if(isset($url) && !empty($url)) { 
    header("location:$url"); 
} 
ob_flush(); 
?>