128
votes

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

1
"(output started at ............ on line 23" Whats on line 23? - Paul Dessert
Line 23 is the 'session_start();' bit of the code above. - user1039878
In my case I had to set the file encoding without BOM. - Wessam El Mahdy

1 Answers

259
votes

"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).