session_regenerate_id(): Cannot regenerate session id - headers already sent

I have several unit tests for my controllers, and all throw an error with PHPUnit (v3.5.14) whenever they include a call to Yii::app()->user->login($identity);

I have searched through my code, and don’t see any extraneous whitespace or echo/print statements, so I’m not sure what’s causing the problem. If I comment out the login() call, everything works fine.

Has anyone else run into this?

"headers already sent" means that you have echoed before starting session or before setting header param. So be carefull echoing(or print) that where are you doing it.

Yes, thanks, I’m quite familiar with the error, as I’ve developed PHP for over a decade. :P What I can’t figure out is what is causing the error. As far as I know I have no trailing whitespace (or closing PHP tags, for that matter) in my files, and I’m not performing any echo/print statements with logging users in, so I don’t know why the login() action would be breaking (when it tries to store a cookie).

Having a similar issue - did you find the problem?

Not yet. :( Running PHPUnit with --stderr works for me, but that just hides the error, it doesn’t fix it.

Maybe it’s an issue of setcookie():

session_start();

setcookie(…);


Yii::app()->user->login($identity);

I think (don’t check it), this send header and Unittest send header earlier. Solution which works for me was use


ob_start();

before test class.

I have the same problem.


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

error.

What’s going on? why echo matters?

Check maybe you file has UTF-8 BOM if you are using notepad ++ check this screen shot, were you can find changing utf-8 without bom.

http://i1-win.softpedia-static.com/screenshots/Notepad-plus-plus_5.png

I’ve the same error when calling


Yii::app()->user->login($identity)

No file encoding problem my side.

Anyone has solve the issue ?

Yii::app()->user->login($identity);

This action indirectly start session, so, maybe you trying to start session on some other place in code?

Maybe try to turn on YII_DEBUG, and you would get more info about error.

Already tried and it doesn’t work neither help :(

Have the same problem.

If comment string in CWebUser->changeIdentity




protected function changeIdentity($id,$name,$states)

	{

		//Yii::app()->getSession()->regenerateID();

		$this->setId($id);

		$this->setName($name);

		$this->loadIdentityStates($states);

	}



all works fine

I don’t think it solve the problem, it skirts it.

It’s a classic PHP issue (not related with yii).

Most of time it’s a whitespace or weird char (sometimes due to bad file encoding) before the first


<?php

For more information, ask google for "header already sent" error message in php.

I hope it could help…

Yes you are right, i have 1 symbol(strange dot) before <?php, in PHPStorm it was hidden, but NetBeans show it. Problem was with file encoding.

I ended up with the same problem, and found it was I because I had (in error) deleted a ?> at the end of a one of my PHP file. In this case at the end of the root index.php file.

Added it, problem solved…

So for me, it wasn’t an erroneous character, it was the lack of ones needed. Tough to debug!

A solution to poster’s actual problem:

I think this turns out to be an issue with PHPUnit because of how it writes out test information. Any headers written during testing will cause this error. I solved this by simply testing for the presence of this error since it meant a successful login:




$this->setExpectedException('PHPUnit_Framework_Error_Warning');



Another solution is to make the test run in a separate process, so the previous PHPUnit test output doesn’t trigger the error:




/**

 * @runInSeparateProcess

 */

public function testSomething() 

{

  ...

}



Another solution. Before the call to Yii::app()-&gt;user-&gt;login add the following code:


$mockSession = $this->getMock('CHttpSession', array('regenerateID'));

Yii::app()->;setComponent('session', $mockSession);

This will override the regenerateID method.

This error occurs because PHPUnit outputs the progress as it’s running the tests.

I received this error after upgrading from 1.1.7 to 1.1.10 (regenerateID was added in 1.1.8 ).

hi

you can put the code <?php ob_start();?> at the fist line of the startup page of the project index.php before reqiure any app

Thanks you, i got it.