129

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

Community
  • 1
  • 1
user1039878
  • 1,345
  • 2
  • 8
  • 6

1 Answers1

261

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

houbysoft
  • 32,532
  • 24
  • 103
  • 156
  • 18
    Put above everything and it worked, thank you! :) – user1039878 Jan 11 '12 at 00:57
  • 85
    Please allow me to share another insight. If you do PHP documents, ensure that the – bryan.blackbee Feb 17 '13 at 11:21
  • 26
    In my case - I created new document, pasted the contents and works. The problem was that in the file included there was a utf sign (bom) included. – Michal - wereda-net Jun 25 '14 at 15:19
  • 4
    I needed to change the encoding of my document from UTF-8 to ANSI. UTF-8 has a hidden [Byte Order Mark](https://en.wikipedia.org/wiki/Byte_order_mark) prior to the start of the file's contents, so that was getting sent before session_start had a chance to do its thing. – AaronSieb Sep 21 '15 at 19:36
  • 3
    I had same issue as AaronSieb. Changed file encoding to UTF-8 without BOM and everything worked! – GiorgiTBS Feb 12 '16 at 07:03
  • 1
    Make sure you are NOT using notepad.exe for saving (editing) such a files. It inserts UTF8 BOM at the very beginning of the file. It usually causes such a php warning with line number 1. – lukyer Mar 25 '16 at 19:32
  • in my case removing end of line spaces from an include solved the problem; (vim) :%s#\s\+$## (also see bryansis2000 above) – zzapper Apr 26 '16 at 10:15
  • in my case, there was a character "p"before the – vishwakarma09 Jun 13 '17 at 09:17
  • @user1039878 Is that really all I have to do? I have the same problem. Session_start is connected to oauthcallback.php I guess? – kelly Aug 28 '17 at 08:16
  • In my case I were using Session_start instead of session_start – M at Dec 24 '17 at 22:35
  • I changed the encoding utf-ascii of my file and it worked. – Jaskaran Singh Jan 14 '18 at 19:12
  • @bryansis2010 was searching whats the issue because i have already put the session_start() at top but still that was not working. Your comment solved a huge problem for me. – arslion Feb 09 '18 at 10:40