开发者

php script redirecting code problem

i'm using a php script for redirection after detecting the search word to my websiter from the search engines .

and my redirection code is working fine

but for some keywods i wat to stay in the same page. for that lines of code i'm getting a warning message in that pages.

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

below is the code i used in that pages

$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(st开发者_运维百科rstr($ref,"how+to+market+better")){
  }    
}

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


Redirections are accomplished by setting an HTTP header, as the use of the header() function suggests. That means that you can only redirect before you start the document output. Whatever you start printing on line 2, do it later ;-)


remove any output started at the 2nd line at index.php


If you are using the Header function you are not allowed to output something before that function is called.

In you case you wrote in line 2 on index.php something to output.

A bad workaround is to use the output cache functions ob_start. but this is not a real solution.


Try this

<?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();
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜