Solved

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

Posted on 2004-04-20
7
70,389 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

11 Experts available now in Live!

Get 1:1 Help Now