Hi,
I have a php file session.php:

<?php

require_once("functions.php"); 
session_start();
...
?>

I tried to include this in one of my php scripts, but it shows me a warning:


Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at E:\xampp\htdocs\cjenovnik1\uredi_profil.php:1) in E:\xampp\htdocs\cjenovnik1\includes\session.php on line 4

I read about this on google. They said there must be no whitespaces or any informations
sent to the browser before session_start().

I modified that but it doesnt't work either:

<?php require_once("includes/session.php"); ?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php confirm_logged_in(); ?>
<!DOCTYPE html>
<html>
<head>

This is part of the code of file uredi_profil.php. In that page I get this warning.

User go to that page by clicking of link "Promijeni podatke" on page adminpanel.php
In page adminpanel.php there is also included session.php (require_once...)

Simply reverse the 2 lines.

<?php

session_start();
require_once("functions.php"); 
...
?>

Or if that is not an option, use ob_start() and ob_flush() at the top and bottom, respectively, of your page.

<?php
ob_start();

require_once("functions.php");
session_start();
...
ob_flush();
?>

Thank you...

I tried both of that, but neither of that isn't working.

I already included session.php in few scripts and it worked perfectly, but now
it makes a problems.

I appreciate any kind of help

The reason it says it's failing is because something is being echo'ed, printed or in short, an image or text is being processed onto the page. When this happens, the error you are getting will be thrown.

Making sure you call session_start() before ANY information is sent guarantees that no headers can be sent before it's initialized. Meaning this must be placed before the include for functions.php if you're displaying anything within it.

ob_start() starts a buffer that grabs everything that would normally be immediately sent to the user and stores it, waiting for ob_flush() to be called. Using those 2 prevents any kind of headers from being sent once the first bit of image or text is sent as well. This should always be placed on the first line or at the very least, be the very first thing called before anything else.

The following will not work and will error as something was displayed before the session was started.

text stuff
<?php
session_start();
...
?>

This will work as it's being called before.

<?php
session_start();
...
?>
text stuff

The same applies for ob_start() while ob_flush() should be the last thing you call when your script finishes executing.

[Edit]
If after you've confirmed that you're using it in the correct manner then you should check to see if a page is being prepended when your script executes as this will cause the error as well.

I have the same problem and am sending no output before the session_start call.

I finally created a stripped down test file and STILL get the same problem.

My test file has ONLY this code:

<?php
$value = session_start();
echo "Session value = ".$value."<br>";
?>

As suggested in various sources:
1) There are no spaces, html, etc. preceding this.
2) The file is encoded in UTF-8 WITH BOM
3) I've modified my php.ini file to set: session.cache_limiter = public

But I still get these errors:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\feralpots\eNorman\test.php:1) in C:\xampp\feralpots\eNorman\test.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\feralpots\eNorman\test.php:1) in C:\xampp\feralpots\eNorman\test.php on line 2
Session value = 1

I am baffled !

Does anyone know how to fix this ??

Thx !

I don't think you can assign session_start() to a variable or at least I've never seen it done like that.

session_start() simply tells the server/client browser to start storing information for that specific session. After it's been started you should use $_SESSION to access the different variables unless you are requesting say the session_id, which could be done like so.

<?php
session_start();
$id=session_id();
echo $id;
?>

Look through the first link below, specifically the session functions section. You probably won't ever use most of those functions but it'll give you an idea of how to use the ones you want to use.

[Links]
http://www.php.net/manual/en/book.session.php
http://www.php.net/manual/en/session.examples.php

Nyight,

Yes this is valid.

The problem was that I was using UTF-8 WITH BOM and for some reason (in an XAMPP environment in Windows XP at least) this must be encoded in UTF-8 WITHOUT BOM.

I simply converted to this encoding (in notepad++) and the warning has disappeared.

But -- thanks for responding to post so quickly !!

What is the configuration file in xampp that have to be changed (coding to utf-8) and
what should be changed?

Thanks in advance :)

aldm,

I'm using notepad++ (great free editor), which has a tab called "Encoding". With this you just select an option to convert to whatever encoding you would like. By the way, ANSI seems to work too.

Have you been using session_start() in someone of the included/required files?
Delete that row and then try again.

Edit:
I read the posts above again and went over something about XAMPP..
That webserver has never been more then problem for me, I'd recommend you to try another one.

VertrigoServ is very good and easy to modify/understand.

1. Make session_start the first line of your code.
2. Remove the blank line after "<?".
3. Change the line "session_start()" to "@session_start()".

I believe changing session_start() to @session_start() just suppresses the warning.

Have you been using session_start() in someone of the included/required files?
Delete that row and then try again.

Edit:
I read the posts above again and went over something about XAMPP..
That webserver has never been more then problem for me, I'd recommend you to try another one.

VertrigoServ is very good and easy to modify/understand.

Once I got through the initial configuration (not hard with all of the online forum help) XAMPP has been great for me. I've been using it for over 2 years now.

As far as I am concerned, this thread seems solved if you attend to the following steps (all covered somewhere above). Please let me know if anyone disagrees:

1) Make sure session_start() is called before ANY other information is sent. Place it before any includes.
2) If this doesn't work, create a stripped down test file and test it again. If the stripped down test file does not work, try changing to a different encoding. In my environment "UTF-8 With BOM" DOES NOT WORK, while either "UTF-8 without BOM" or ANSI works fine.
3) If there is some reason (I can't think of one) to output something before calling session_start(), you might try putting ob_start() at the beginning of the file and ob_flush() at the end. This buffers the output and may allow session_start() to work -- but this is more of a work-around than a solution.
4) Using @session_start() does not solve the problem. It simply supresses the warnings so that you don't see them.

Does this solve the problem to everyone's satisfaction ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.