redirect() problems

Hi,

on my testsystem is everything fine working, but on my production system, the redirect() method is nothing doing!!!!

i only get the error "Header alreday sends…"

anybody an idea?

This happens when output is written before the redirect. So make sure nothing is echoed etc before the redirect.

yeah i know,

but i have no echo’s and no print_r before!!!

the strange thing is, that on the test-system (windows 7) everything is ok, only on the production system i have problems (debian)

2009/11/13 04:03:41 [error] [php] Cannot modify header information - headers already sent by (output started at /var/www/blog/protected/config/main.php:1) (/var/www/yii/framework/web/CHttpRequest.php:509)

Stack trace:

I’ve had that before. You php files are messed up (maybe wrong charset or whatever).

Open main.php in your favorite php-editor, copy everything, paste into a new file and save. Upload and try again.

Also check transfer mode of your ftp-app. Maybe it needs a change to binary or something.

my whole workspace is in utf-8

I have load the main.php in my editor and encode it to UTF-8 and uploaded it… nothing

my STACK TRACE


 	Cannot modify header information - headers already sent by (output started at /var/www/xyz/protected/config/main.php:1) 

	(/var/www/yii/framework/web/CHttpRequest.php:509) Stack trace: 

	#0 /var/www/blog/protected/controllers/EngineController.php(74): EngineController->redirect() 

	#1 /var/www/yii/framework/web/actions/CInlineAction.php(32): EngineController->actionSearch() 

	#2 /var/www/yii/framework/web/CController.php(300): CInlineAction->run() 

	#3 /var/www/yii/framework/web/CController.php(278): EngineController->runAction() 

	#4 /var/www/yii/framework/web/CController.php(257): EngineController->runActionWithFilters() 

	#5 /var/www/yii/framework/web/CWebApplication.php(310): EngineController->run() 

	#6 /var/www/yii/framework/web/CWebApplication.php(120): CWebApplication->runController() 

	#7 /var/www/yii/framework/base/CApplication.php(135): CWebApplication->processRequest() 

	#8 /var/www/blog/index.php(11): CWebApplication->run() REQUEST_URI=/engine/search.html?value=xyz

edit

if a call directly the URL, than is everything fine working. I have only problems with redirect()


else if ( preg_match( $pattern_alpha, $searchword ) )

{

	$this->redirect(array("engine/phrase", "value" => $searchword));

}

Some binary data you can’t see in a normal editor is in your main.php. Open the file in hex-editor and you will see. Either it’s utf8 BOM or something else, but you need to get rid of it to get it work correctly.

i don’t know where, but yes, there was an invisible character.

what editor are you using, to find so a invisible character?

edit

if i’m uploaded the file on my system, the failure is coming back!

i’m using Filezilla

Hex-editor? Have none installed, just remembered some month ago I had the same problem. Opened with some shareware hex-editor I found via google and fixed it.

I don’t have FileZilla installed, but I guess you have to change the transfer mode (try binary).

I have switch my Workspace from Windows 7 to Linux Ubuntu.

After that I have uploaded the files (under Ubuntu) with filezilla.

And everything works fine ;)

thx @ Y!!

You could also change your php.ini settings to have output buffering turned on by default. This would have solved the redirect problem.

However, because of that random character (I haven’t run into it on my windows 7 stuff and I’m using utf8 and filezilla) you may have had some interesting display issues even after the output buffer configuration was changed.

In general, though, I find it a good idea to turn output buffering on by default in the php.ini file. That’s just me, though.

You can remove the UTF-8 BOM from the output using the ob_start function. This way you can leave the UTF-8 BOM in your source files so your editor understands it is really UTF-8.

In the /protected/config/main.php you have to add before returning the config array:




ob_start('My_OB');

function My_OB($str, $flags)

{

    //remove UTF-8 BOM

    $str = preg_replace("/\xef\xbb\xbf/","",$str);

    

    return $str;

}

return array( ... yii config array ...);



P.S. You don’t have to call ob_end_flush(), php will do this automatically at the end of the script.

You might have a whitespace after the closing php tag ?> in your main.php

This solution worked for me.

Is this a good solution or could this cause for problems?

Does this mean I have an UTF-8 BOM? Is there a terminal batch script to verify this? Or within the IDE (I’m using Dreamweaver CS 5 for Mac)

Hey

I had the same problem with you. When I started writing a new file, it’s encoding was ASCII adn when I changed it to UNICODE I had this kind of problem. I am using Notepad++ and it has an option under “Encoding” which says “UTF-8 without BOM”. I used to just choose encode in “UTF-8” and not UTF-8 without BOM" and that’s why I had this problem. BOM adds three bytes in front of the file which are not recognized as encoding characters but as true output.

I don’t know how you can change your file encoding through other editors but Notepad++ is quite helpful in that!

This is the link about BOM.

Just had the same issue, but my problem were 2 whitespaces right after the php closing tag in one of my model files. So if you get this error search for anything outside your php tags

thanks. yes is the format of the file utf8 or ansi, etc. you can not change it just in a few files. you have to be consistent. use geany in linux or notepad ++ in windows.

Hi there,

I ran into the same issue but in my case everything was in UTF.

The problem was in one CController, it had two spaces into the beginning of the controller.

Attaching a shot to illustrate.

This led to my solution… Dave, you rock!

I enabled logging of errors and tried to reload the offending page.

protected/config/main.php

array(‘class’=>‘CWebLogRoute’,), // to enable debugging

This told me which file to look at…

I then opened that file, deleted lines after closing ?> tag and my problem (Captcha was failing to generate) went away!

Buys Dave virtual beer!