"Cannot send session cache limiter - headers already sent"

user1039878 picture user1039878 · Jan 11, 2012 · Viewed 470.6k times · Source

Having a problem with sessions which is becoming very annoying. Every time I try to start a session on a particular page I get the following error:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at ............ on line 23

using this code:

<?php
session_start();
if(isset($_SESSION['user']))
    {
        $user = $_SESSION['user'];
        echo "$user";
    }
else
    {
    }
?> 

Is it suggesting I've already used session_start(); ? Ive had a look around but nothing really clears this up for me.

Thanks

Answer

houbysoft picture houbysoft · Jan 11, 2012

"Headers already sent" means that your PHP script already sent the HTTP headers, and as such it can't make modifications to them now.

Check that you don't send ANY content before calling session_start. Better yet, just make session_start the first thing you do in your PHP file (so put it at the absolute beginning, before all HTML etc).