-2

i have a file 'header.php' which has a search text box in it. Another file called 'myPage.php' includes 'header.php'. In header.php

 if (isset($_POST['Searchbutton'])){ // this will execute when the search button is clicked.
 $target = $_POST['searchtext'];
 header("Location: searchresult.php?text=" . $target); // line #8
 }

In myPage.php

<?php
include("header.php");
?>

When i'm on "myPage.php" and use the search option, i get an error that says

Cannot modify header information - headers already sent by (output started at myPage.php:3) in header.php on line 8.

Can anyone please help me understand the concept. I'm a newbie to php. Please ask if any more info is required.

Cheers!

3 Answers3

0

You can not output something to the browser before you send a header(). Just have a look at how http works and you'll understand :)

http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Client_request

pmayer
  • 341
  • 2
  • 13
0

In short, what you're trying to do doesn't make sense. When you send a page, you also send the HTTP headers with it. You are trying to send a page, then send a header that redirects the client to another page.

This is why you get the headers already sent error. Since you're just trying to post a form, there is no reason to try and redirect... simply have the target of the form be the script that processes the search criteria.

gview
  • 14,876
  • 3
  • 46
  • 51
0

header() can not be called if anything has already been output to the browser. You can avoid this problem with ob_start() at the beginning of the code, and then ob_flush() where you want to send the output. This will buffer all the output to the browser until ob_flush() is called.

This also means that header() redirects, or other changes can be made as long as they occur before ob_flush()

Camron
  • 308
  • 1
  • 7