Link to home
Start Free TrialLog in
Avatar of amintushar23
amintushar23

asked on

session_start(): Cannot send session cookie - headers already sent by

here is the script i used

<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<?
session_start();
session_register("abc");
$abc="hello world";
echo "The content of sess variable is $abc";
?>
<a href="call_session.php">Next page</a>
</body>
</html>


error shown is :

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at c:\php_project\session.php:7) in c:\php_project\session.php on line 8

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at c:\php_project\session.php:7) in c:\php_project\session.php on line 8
The content of sess variable is hello worldNext page

can u please help me out

ASKER CERTIFIED SOLUTION
Avatar of jkna_gunn
jkna_gunn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of movemedia
movemedia

jkna_gunn is right, and just to add to that... PHP is very strict about that, so if in the future you do this, but you find you are still getting the error, check for any whitespace before the session_start().  even something like:

   <?
session_start();
$_SESSION['abc'] = "hello world";?>
...

could cause the problem.  Also, check any include files you might use (it is best to put session_start() above the include files anyway).  
try put this at very beginning of your script
<?ob_start();
session_start();?>

<HTML>
.... the rest of your script....
Another thing to keep an eye out for is when including files in your php pages and using sessions, you need to make sure that any of the files included before you issue the session_start() do not contain anything outside the PHP tags, e.g. blank lines, spaces etc as this will result in the same error and catches a number of people out.

For example.

<?
include "config.php";
session_start();
?>

the config.php file should not have anything outside of it's <? ?> tags, otherwise it'll result in the same error. And as mentioned it's preferable to issue the session_start() before you do your includes, however should a situation arise where you need to include other files before starting the session it might be handy to check them for blank lines.

Cheers
I am testing the folliwing code and getting the same error..
Warning: session_start(): Cannot send session cookie - headers already sent in C:\apache\htdocs\sessionTest.php on line 1

<?session_start();
if ($_SESSION["last_visist"]){
echo "Date of las visit: ";
echo date("j F Y, H:i:s", $_SESSION["last_visit"]);
echo "<br>";
}
else
    echo "This is your first visit";
    $_SESSION["last_visit"] = time();
    $_SESSION["num_visits"]++;
?>
Just wanted to say thanks for this tread and the simple explination. Helped me out a lot,
Thanks again for a great site.

mark
www.sunspeks.com 
Thanks for the solution on this, I figured out right away that I need to have my include file at the first line of the page but continued to receive errors, I had indented the php block to set it off from the rest of the page, the suggestion about removing white space fixed my errors, thanks