1

Possible Duplicate:
Headers already sent by PHP

These are the errors:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers
already sent by (output started at 
C:\xampp\xampp\htdocs\ProjSecond\includes\overall\Oheader.php:3) in  +
C:\xampp\xampp\htdocs\ProjSecond\core\init.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter -  
headers already sent (output started at  
C:\xampp\xampp\htdocs\ProjSecond\includes\overall\Oheader.php:3) in  
C:\xampp\xampp\htdocs\ProjSecond\core\init.php on line 2

I am structuring my project in different directories, heres the code of the relevant files:

FILE: init:

<?php
session_start();
require 'database/connect.php';


?>

FILE: Oheader:

<body>
<?php include 'includes/header.php';?>

FILE: Head:

<head>
<title>Project47</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="css/pjcss.css">
</head>

Does anyone have any idea of why this is happening... the codes showed above are the entirety of each file

Community
  • 1
  • 1
Cesar Davi
  • 21
  • 1
  • 4

3 Answers3

4

its because you have output something before start the session

even the white space before the session start cause this warning

and header already sent is also because of something is sent before header

enter image description here

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • Be weary of trailing spaces in included files as well. If they are included before output is expected it can lead to similar errors. A trick to avoid this is to leave php open at the end of the file. I.E., don't put the closing ?> in files like database/connect.php – Joshua Kaiser Oct 24 '12 at 06:01
  • 2
    Just to add an information that might help other people: If you made sure there isn't any character written before `session_start` and the error persists then you're probably saving your file with a BOM (Byte Order Mark), in your editor there should be an option to choose an encoding like `UTF-8 without BOM` – Delta Oct 24 '12 at 06:02
1

With your project structure, you need to make sure that init.php is the first file called in the execution sequence, and every other file, including the "Head", should be included inside that file. You cannot output or even an empty string before the session_start(). If all files are included in a separate main file, say index.php, it should look like this.

  <?php
      include ("init.php");
  ?>
   <html>
   <!-- The head file should come here -->
   <?php
      include "Oheader.php";
    ?>
janenz00
  • 3,315
  • 5
  • 28
  • 37
0

Write the php codes and the include statements at the top. And then start writing the html. See below

<?php
session_start();
include 'includes/header.php';
?>

<html>
  <head></head>

...
...
Abhishek Saha
  • 2,564
  • 1
  • 19
  • 29