Warning: session_start() [function.session-start]: Cannot send session cache limiter

Hello sitepoint,

I am new to PHP, but yet I have decided to try and tackle a huge obstacle. To learn and to program PHP. Anyways I have been using an example in my PHP book, and to modify it in my own terms. Anyways I have a problem, and not exactly sure how to proceed from here. I’ve read that session_start(); must go before all html code and must have no white space. I believe this is true for my site but not sure. Maybe a second pair of eyes and more knowledge could help me understand why I am getting this warning. Here’s my Warning- Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/YourSite/public_html/news.php:9) in /home/YourSite/public_html/header.php on line 3

<?php //news.php
include_once 'header.php';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<HEAD>
<TITLE>Welcome to YourSite!</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<?php // header.php
//ob_start();
session_start();


include 'functions.php';

if (isset($_SESSION['user']))
{
	$user = $_SESSION['user'];
	$loggedin = TRUE;
}
else $loggedin = FALSE;


echo "$appname";
if ($loggedin) echo " ($user)";
?>
<div align='center'>
<?php 
if ($loggedin)
{
	echo "<p><b>$user</b>:
		 <a href='members.php'>Members</a> |
		 <a href='friends.php'>Friends</a> |
		 <a href='messages.php'>Messages</a> |
		 <a href='profile.php'>Profile</a> |
		 <a href='logout.php'>Log out</a></p>";
}
else
{
	echo "<p><a href='signup.php'>Register</a> |
		 <a href='login.php'>Log in</a></p>";
}
?>
</div>

It may be a copy&paste error, but it seems you have a space before <?php in both php files.

I don’t think there’s any white space, I don’t believe that is the problem. Hmmm… Also I don’t see any white space in my post either. So where do you mean exactly if you say you notice white space?

Try moving the session_start() to the news.php file, straight after the <?php

I tried your script and it works fine?

Try getting into the habit of adding these lines to the first PHP file that is called.


<?php 
  
  error_reporting(-1);
  ini_set('display_errors',1);
  session_start();



I checked, and it’s the forum that displays a space in front of the first line of code (at least in my browser).

Do take a look at this answer in another thread about the same problem, it might have to do with the BOM.

Alright Seems like this code worked


<?php   
error_reporting(-1);
ini_set('display_errors',1);
session_start();

But now I have a question, is it possible to use this code in any place other than at the very top? Because I notice that some of my Styles aren’t working with my CSS.

<link rel="stylesheet" type="text/css" media="screen" href="navigationbarNEWS.css">

I have a dropdown menu that doesn’t drop down, but the normal navigation works fine. Unless you need to see, I could post an example.

Also if I put my

<?php include_once 'header.php'; ?> 

after the <body> my Navbar works fine, but then I get those session_start(); head cache limit warning.

If your style sheets are not working then it looks as though they are not being loaded.

A quick test is to “View source” in your browser and to click on the link; which should open your CSS file. If it does not open your CSS file then check the relative path from your calling program to the CSS file.

You answered your own question in the first paragraph of your first thread post :slight_smile:

I’ve read that session_start(); must go before all html code and must have no white space.

Programming is an exact science with no gray areas. There is only one answer unlike art, sociology, history, etc

Alright I just don’t know why my Styles aren’t working though, when I use the <?php include_once ‘header.php’; ?> at the top. So that’s why I was testing it in the body, but that gives the Warning: Function_start issue. I’m not sure what to do. I even changed the file to .php instead and included. For the most part all of the styles do work except my dropdown menu. That’s the only thing not working… but without any php <include> at top everything works out fine. So I don’t get it… Unless I use <include> in the body then my dropdown menu works.

I checked and the reason why I had no problems was because I used your files and hard-coded the $logged-in test. When a validate user is detected then your script echoes text and causes the problems.

Take a look at this template:

  1. note the comments
  2. test the validation links.
  3. use and enhance this template and continually validate your script.

http://johns-jokes.com/downloads/sp/session-problems/

I don’t have a problem with the sessions working, just my CSS dropdown… Not sure… I’ll try that link though.

Your ‘header.php’ script creates html code that has to be inside the <body> part of the page. So the include of ‘header.php’ has to be put inside the <body>. But that means, of course, that you can’t put session_start in ‘header.php’, because you’ll get a ‘headers already sent’ error.
If you don’t want to put the session_start hard coded at the top of the news.php script, create a new script that contains the session_start, and include it at the top of the news.php script.

Alright I got it working… :slight_smile: And just for future references I’ll post the solution… GUIDO was right I needed to change the location of the session_start();, I kept it at the very top, made a file called startsession.php and included session_start(); then included header.php after the body. Works perfectly now… All my css and php all working good together now.

<?php
include_once 'sessionstart.php';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<HEAD>
<?php //startsession.php
error_reporting(-1);
ini_set('display_errors',1);
session_start();
?>
</HEAD>
<BODY>
<?php // header.php
include_once 'header.php';
?>