Saturday, January 29, 2011

My PHP Server is giving session errors all of a sudden

I am running Xampp which is a LAMP setup basicly but for windows. I have been using it for years now with no trouble and all of a sudden, all my sites pages that use PHP sessions are now giving errors like this...

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\webserver\htdocs\friendproject2\labs\2.php:1) in C:\webserver\htdocs\friendproject2\labs\2.php on line 3

I realize that this happens from 1 of 2 things generally.

  1. If there is any whitespace printed to screen before session_start() function is called, that can sometimes cause this to happen
  2. If there is anything printed to the screen/browser before the session_start() is called.

Now my problem is different. Before tonight, I had hundreds of files that used sessions and none of them showed any of these errors. It is not just 1 file where I am overlooking a user error, this just started affecting all my files. I have not made any changes to my computer tonight or recently that I recall either.

What could be causing this? It is driving me insane and nobody seems to know why this started happening. I think it must be server related

I can even create a file and put it into any folder of my servers web and be a simple file like this bvelow and it will still give the error I show above....

<?PHP
session_start();

$_SESSION['test'] = 'test value';

echo $_SESSION['test'];
?>
  • Check if all your files are saved with UTF-8 encoding. UTF-8 encoded files may include a BOM (Byte Order Mark) to tell the difference between big endian/little endian byte order. PHP does not understand BOM and when it hits that in the begining of the file, it assumes it's dealing with data and sends it off - by then it's too late to modify headers.

    The solution would be to make sure you save your files as ANSI - configure your IDE/editor

    Hope it helps.

    EDIT:

    If this is the case, you probably have a lot of files that you need converting. You can use try using this bash shell code that uses iconv to do it for you (adapted from : http://stackoverflow.com/questions/1182037/osx-change-file-encoding-iconv-recursive)

    for files in /mydisk/myfolder/*.php
      do
        iconv -f UTF-8 -t ISO-8859-1 "$files" "${files%.php}"
    done
    
    From

0 comments:

Post a Comment