-2

I'm getting this warning:

Warning: session_regenerate_id() [function.session-regenerate-id]: Cannot regenerate session id - headers already sent

But the first this I do is actually ob_start..

Test script:

ob_start();
$session = new SessionClass();

echo "Test";

SessionClass:

function __destruct() {
    session_regenerate_id(true);
    session_write_close();
}

Ans at least, the constructor of the class:

    session_set_save_handler(array(__CLASS__, '_open'),
                             array(__CLASS__, '_close'),
                             array(__CLASS__, '_read'),
                             array(__CLASS__, '_write'),
                             array(__CLASS__, '_destroy'),
                             array(__CLASS__, '_gc'));
    session_name($sessionName);
    session_start();

The Test script should do an output to the JSON by ECHO "Test". But, it doesn't...

Joran Den Houting
  • 3,149
  • 3
  • 21
  • 51

1 Answers1

1

See PHP docs for __destruct(): http://php.net/manual/en/language.oop5.decon.php

Destructors called during the script shutdown have HTTP headers already sent.

session_regenerate_id() tries to send a cookie with the session id, but since you're calling it in a destructor the headers are already send - hence the error message.

Why do you need to regenerate the session id? Your code suggests that first you start a session, generate an id, send some data and then regenerate the session id before closing it. Unless you have a specific reason for this, you don't need to do that at all.

Paulina
  • 554
  • 3
  • 8