Solved

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

Posted on 2004-04-20
7
70,407 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Site hacked - decoding the PHP? 15 72
unset shopping cart session 15 33
Preventing users manipulating URL strings 10 37
datetime in sql 6 31
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now