We help IT Professionals succeed at work.

header already sent

mammouth asked
i want to make a redirection to another page. bug i get header information already sent error.

How can i bypass this error.

Note.: i need to make the redirection in middle of the page
Watch Question

Before using header, you must not send something into the browser.

<?header("Location: test.php")?>
won't work

do you really need a redirection? maybe you can the include function or use some javascript
Top Expert 2005
You could also redirect your page using Javascript or html

Using Javascript:

or with HTML

<meta http-equiv="REFRESH" CONTENT="0;url=newpage.html">

An error I experience using the header() function is that there's a empty line at top of my php-file, ie: before the first <?php sign.

Before header information pass their must not anthing which write on the page

<?echo "test";
header("Location: test.php");?>

will give error

<?header("Location: test.php");?>
also give error

you must note that before header nothing should present which write on page

header("location: index.php?word=$test");?>

will work

Top Expert 2005

Redirect at middle of page:

Use Javascript or HTML. No other things will work.

You could redirect your page when it's loaded like this:

<body onload="location.href='newpage.html'">


header ("Location: http://www.php.net"); /* Redirect browser
                                            to PHP web site */
exit;                 /* Make sure that code below does
                         not get executed when we redirect. */

Remember that the header() function must be called before any actual output is sent, either by normal HTML tags blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that will output before header() is called. The same problem exists when using a single PHP/HTML file.
<?php require("user_logging.inc") ?>

<?php header ("Content-Type: audio/x-pn-realaudio"); ?>
// Broken, Note the blank lines above

Explore More ContentExplore courses, solutions, and other research materials related to this topic.