Solved

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

Posted on 2004-04-20
7
70,482 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

695 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