5

I am learning about handling UTF8 character sets and the recommendation is to explicitly set the encoding type in the output headers within your PHP script like so:

header('Content-Type: text/html; charset=utf-8');

My question is about where I should place this header. I have a config file that is included into every script and is run first. Should I place it at the top of that file so that this header is included first in every php file?

Will that affect my ability to set other headers, including a header location redirect down the line? Or should I place this just before any html output, such as the rendering of my template file? Ie- can this header be in place before ALL other php processing, and what are the consequences of doing that? Does it affect anything server side, or just the encoding of the output?

Thanks.!

Sherri
  • 816
  • 2
  • 9
  • 18

5 Answers5

4

Although early setting of this header will not affect anything server-side nor other types of headers, your question being asked out of assumption that the only content your code going to output is of html type.

However, general PHP application sometimes outputs content of some other types, like application/pdf, application/excel or whatever.

Thus, sending it just before html output seems to be exactly the right place.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • What if I remove the content-type part and only specify the charset? – Sherri Mar 22 '11 at 12:44
  • And wouldn't re-setting this header before outputing a zip or CSV file for example, solve this dilemma? I just want to make sure all output is UTF8, by default. Then I can change the encoding when necessary. – Sherri Mar 22 '11 at 12:48
  • @Sherri re-setting it would be just dirty hack. Why don't you want to set it at the right place - before starting template rendering? If you want to make sure you always send it, set it up in web-server config – Your Common Sense Mar 22 '11 at 12:51
  • @Col. Shrapnel - yes that's a good point. I was just hoping to set the default encoding in one place, then reset as needed. I don't have a nice place to put it where it would happen before ALL output rendering (some output does not use the template - ex error log page, 3rd party plugins, etc). – Sherri Mar 22 '11 at 12:56
  • @Col. Shrapnel: Nicely explained, +1. – Michiel Pater Mar 22 '11 at 12:59
  • @Sherri: You could use output buffering? – Michiel Pater Mar 22 '11 at 13:15
3

The only constraint you have is not to output anything (print, echo, ...) before you call the header function. Apart from this, you can call that function anywhere you want to and any number of times (you can set other headers afterwards).

Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
3

I'm only recommending this as an alternative. But the setting can be fixated in the php.ini using:

default_mimetype = "text/html; charset=UTF-8"

It's less common on shared hosters, where some of the users might depend on L1. But if you use your own server and want to ensure that it's correctly set regardless of code and whichever PHP script is invoked, then this is your option.

mario
  • 144,265
  • 20
  • 237
  • 291
  • Not sure about specifying charset in `default_mimetype`, but at least by default value of [`default_chartset`](http://php.net/manual/en/ini.core.php#ini.default-charset) is used for the "charset=..." part. – x-yuri Jan 28 '16 at 08:58
1

You can put the header directive anywhere prior to when you start sending page text. The order relative to other headers is not supposed to matter; however, if you have sent even one character of page text, PHP will give you an error.

Note that "sending page text" can sometimes happen inadvertently due to another error message or due to a space before or after <?php ?> tags, etc.

Andrew
  • 14,325
  • 4
  • 43
  • 64
0

As long as it is inside the -tags and BEFORE you create any output (echo, print, print_r and so on - or any HTML BEFORE the -tags).

OKAY METHOD:

<?php
$var = "hello world";
$var2 = "Goodbye world!";

header();

echo $var2;
?>

OKAY METHOD:

<?php
header();
$var = "hello world";
$var2 = "Goodbye world!";



echo $var2;
?>

NOT OKAY METHOD:

<?php
$var = "hello world";
$var2 = "Goodbye world!";

echo $var2;
header();
?>

Hope this helps!

Dennis Lauritzen
  • 404
  • 3
  • 7
  • 21