0

I have the following code in a PHP file called via Ajax (note - the code is a stripepd down version of the actual code, but still produces an error:

<?php
    session_start();
    $response = array(
        'status'=>'abc',
        'a'=>'abc',
        'b'=>'abc',
        'c'=>'abc'
    );

    header('X-JSON: '.json_encode($response));
    exit;

?>

There are absolutely no spaces or anything before the opening

Yet when I call the script I get:

'[11-Aug-2009 14:56:13] PHP Notice: A session had already been started - ignoring session_start() in \test\action.php on line 2

[11-Aug-2009 14:56:13] PHP Warning: Cannot modify header information - headers already sent by (output started at \test\action.php:2) in \test\action.php on line 10'

Line 2 is the session_start() command and line 10 is the header() command.

So PHP is telling me that it has ignored the session_start, and then immediately telling me that actually that line produced output....whats going on?

pauljwilliams
  • 19,079
  • 3
  • 51
  • 79

4 Answers4

3

Well, believe it or not, the criminal output is......

'[11-Aug-2009 14:56:13] PHP Notice: A session had already been started - ignoring session_start() in \test\action.php on line 2

Either suppress error messages (bad idea in development) Or find where you started the session (which is really how you need to solve this)

Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
  • 2
    Gah - yes, you're right. FWIW I work on two different PHP projects, one of which has auto-started sessions, and this one which doesnt. I hadnt changed my PHP.ini, so the rogue error was being output. The problem was compounded by it being an ajax request, so the error wasnt as obvious. – pauljwilliams Aug 11 '09 at 14:14
0

It may be related to this question of mine.

Community
  • 1
  • 1
graham.reeds
  • 16,230
  • 17
  • 74
  • 137
0

Check if there's this annoying little bugger called BOM (Byte Order Mark) creeping in your file. It's possible if you've encoded that file as utf-8, and you can catch it with firebug for example.

JHollanti
  • 2,343
  • 8
  • 27
  • 39
-1

Use ob_start();

Alix Axel
  • 151,645
  • 95
  • 393
  • 500