3 replies [Last post]
ro88o
ro88o's picture
Offline
Enthusiast
Manchester, UK
Last seen: 14 years 1 week ago
Manchester, UK
Joined: 2005-06-29
Posts: 202
Points: 0

Hi everyone.

Ok I have the following two errors on my site while trying to implement a username/password:
Warning: session_start(): Cannot send session cookie - headers already sent by [gives file location info]
Warning: session_start(): Cannot send session cache limiter - headers already sent [gives file location info]

This is the top of my code for index.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>StooFoo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css/stoofoo.css" />
<link rel="stylesheet" type="text/css" href="css/boxes.css" />
</head>

<body>
<?php 
  include 'includes/checklogin.inc.php';
  include 'includes/db.inc.php';
?>
...

and this is the start of my 'checklogin' include file:

<?php 
session_start(); // Start a session

if (!isset($_SESSION['username']) || !isset($_SESSION['password'])) {
	$logged_in = 0;
	return;
} else {
...

Can anyone tell me where my problem might be please ? I read something about not doing any echo statements etc. etc. before starting a session but I can't see any point I'm violating that :?

I may not have posted enough information but I assumed the parts I posted could be the only places where the problems occur, but if I've got that wrong I'd be happy to show more.

Help would be greatly appreciated!
Tom

www.nubornis.com
Web Development and E-commerce Solutions.

Tags:
Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 7 years 19 weeks ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

Cannot send session cookie [SOLVED]

This is really rather off topic even for off topic!

Probably better suited to a scripting/php forum.

You need , I think to ensure that your session_start() is executed before any other output, you have it running after the script runs and generates the page (HTML) move your include portion of the session start script to the top of the file before any other output. Session_start() must be the first thing that is parsed before any other output.

Cannot modify headers is perhaps one of the commonest errors check the PHP manual for more detailed info .

Hugo.

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

ro88o
ro88o's picture
Offline
Enthusiast
Manchester, UK
Last seen: 14 years 1 week ago
Manchester, UK
Joined: 2005-06-29
Posts: 202
Points: 0

Cannot send session cookie [SOLVED]

Quote:
This is really rather off topic even for off topic!

I know but as my favourite forum csscreator always seems to be my first port of call Smile I thought this would be a pretty simple question for someone anyway which it was as I've shifted the include and it fixes both errors - thanks Hugo!

www.nubornis.com
Web Development and E-commerce Solutions.

Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 9 years 35 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

Cannot send session cookie [SOLVED]

The error is a misrepresentation. Althought strictly correct, it would be more meaningful if it said, "can't send any more headers, content has been sent".

To explain.

HTML is stateless, the only way for the php server to know the user has visited before is for the user to send some unique identfying information each time they access the server. That particular magic is handled by the session identifier.

In most modern php pages the session identifier is transferred between the server and the client as a cookie. Within the http protocol, cookies are transferred in the headers. As you would think with a name like headers, the headers come first - before any of your page output. As soon as you have started to output your page, no more headers can be sent.

In your case, as Hugo pointed out, you needed to reorder your code so the session id could be sent before you started your output.

Most of the time, this error crops up in conjunction (and after) another error. The simple act of there being a PHP warning/notice (with display_errors = On) means PHP will send out html content - the error details - and keep on running. Triggering the second warning - of trying to start a session after content has already been sent.

ref: cookie | sessions