-3

Possible Duplicate:
Headers already sent by PHP

my website was running fine till i shifted to the hostgator server , but after switching i found errors on every page .

Cannot modify header information - headers already sent by (output started at /home/flip/public_html/index.php)

i opened up my code and i read some where not to put up html before <?php ?>

my code started like this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php 
@session_start();
include_once("includes/connection.php");
$id=$_SESSION['user_id'];
    if(!isset($_SESSION['user_id']))
    {
      header("location:index.php");
    }
        if (!isset($_GET["event"])) {
            $caseVar = "share";
        } else {
            $caseVar = $_GET["event"];
        }
?>

and it was running fine in previous server but when i switched to new it started giving header errors in each and every page . but later on i put the doctype after the <?php ?> and it started working as before . i really wanted to know why this happened .cant html be written before <?php ?> and will i have to change my code everytime i switch from server to server ?

Community
  • 1
  • 1
Sakshi Sharma
  • 1,414
  • 4
  • 20
  • 39

3 Answers3

2

You cannot call header() functions after output has started. If you want to ensure that no output begins before you call a header(), use the output buffer ob_start() then ob_end_flush() to print the output and clear the buffer.

FYI, session_start() is a type of header() function.

Matt
  • 6,993
  • 4
  • 29
  • 50
1

One cannot have any html before a session_start() is called at the top of a php page. Place the <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> after the closing php tag ?>

Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27
1

You should just search for the error, it is a very common problem

The thing is: you can only send headers once, and if you output ANYTHING, you need headers.

So your line

header("location:index.php");

cannot do anything if you output something first (like whitespace, or in your case this complete line:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Same goes for the session: it needs to send headers.

Nanne
  • 64,065
  • 16
  • 119
  • 163