1

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(strstr($ref,"how+to+market+better")){
  }    
}

if($url !=""){
  header("Location:$url");
}
2
  • You can use the '0101010' button to format code. Otherwise, it's not readable. Commented Apr 29, 2010 at 9:53
  • Also Check U r not Left with any blank space at the end of file after "?>" Commented Apr 29, 2010 at 10:21

4 Answers 4

1

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 ;-)

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

8 Comments

Why are output cache functions not a real solution?
@Manos: because the better is to cure disease, and not just hide symptoms
Dr. zerkms is in Wrong Community, We need a coder to explain :)
@zerkms The "disease" is unavoidable in larger applications, especially OO frameworks, where you have no idea what/when controllers might try to do. Using output buffering is a perfectly acceptable strategy. In fact it's often unavoidable.
@Manos Dilaverakis: 1. he has 10 lines script. 2. OO frameworks always use ideology of chain of responsibility/intercept filters/request-response - so we NEVER EVER could do direct echo, because we have "response" object to interact to user. the ob_* is perfectly ugly and lame strategy
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.