Solved

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

Posted on 2004-04-20
7
70,453 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:amintushar23
7 Comments
 
LVL 6

Accepted Solution

by:
jkna_gunn earned 500 total points
ID: 10867598
you cannot output any html then call session_start(), this must be the first line.

<?
session_start();
$_SESSION['abc'] = "hello world";?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<? echo "The content of sess variable is $_SESSION['abc']";?>
<a href="call_session.php">Next page</a>
</body>
</html>
0
 
LVL 4

Expert Comment

by:movemedia
ID: 10871132
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).  
0
 

Expert Comment

by:mobana
ID: 10874934
try put this at very beginning of your script
<?ob_start();
session_start();?>

<HTML>
.... the rest of your script....
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:robknowles
ID: 13204649
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
0
 

Expert Comment

by:lucasfcohdz
ID: 14432979
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"]++;
?>
0
 

Expert Comment

by:phone-bits
ID: 14560619
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 
0
 

Expert Comment

by:Crestwood
ID: 20788097
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
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question