How to FIX session error

Hi every one,

I’ve started with Yii 2 months and now when I upload my project to hosting I get this mesg:




session_regenerate_id() [<a href='function.session-regenerate-id'>function.session-regenerate-id</a>]: Cannot regenerate session id - headers already sent 



in: framework/web/CHttpSession.php(182)




public function regenerateID($deleteOldSession=false)

    {

        // Error here

        session_regenerate_id($deleteOldSession);

    }



When I use this command:

[code]

Yii::app()->user->login($this->_identity,$duration);

[/code

in: protected/models/LoginForm.php(71): CWebUser->login(UserIdentity, 0)

I’ve try to add on_clean & something like that @@ but It still get error.

Somebody could help me solve it?

Thanks.

i dont really knoow this error but all the errors with "headers already sent" are because you trying to change the session when headers already have been sent.

so first of all check that you dont have a space or newline in the beggining and end of your files, check that you dont have some "echo" command.

I’m still checking my code @@

So hardly to find it…

most of the times it is written more or less where is the content that has been sent(the space/newline)

I’m having the same problem.

Have you resolved this?

Thank You

A quick suggestion to anyone else that has this error like I did just did:

Use the stack trace and place session_regenerate_id() (or any other function that generates headers) at the line above each call in the stack trace - until you find the file that has a whitespace before <?php or after ?> or causes output by echoing etc.

I experienced this error and here is what i do…

add header("Content-type: text/html"); // or any header you want before the error occured.

It will then return a more meaningful error like "Cannot modify header information - headers already sent by (output started at /path-to-file/protected/extensions/PHPPDO/phppdo.php:737)"

Goodluck.

1 Like

Thank you for the tip dellzam, I had the same problem and this helped me to solve it!

Hi all,

i’d like to mention that such a king of issue is the main reason to avoid the usage of PHP closing tags ( ?> ) in the PHP-only files like controllers/models/comoponents etc. (except of views of course). By doing so we eliminate the possibility that some evil space after ?> will consume our time to find it :)

Thank you so much! You have no idea how much insane i made! Thank you!

Thank you very much. It really helped me in figuring out the issue.