Search code examples
php.htaccessheadermeta

is setting php charset necessary when charset is set in htaccess?


Is PHP header('Content-type: text/html; charset=utf-8'); necessary to be set when

AddDefaultCharset utf-8
<IfModule mod_mime.c>
  AddCharset utf-8 .php .html .xml .css .js .json
</IfModule>

is defined in my .htaccess-file? Is the .php-files even affected by this rule?

I'm setting up a settings.cfg.php-document, and wondering if I really need this line of code - if it would be sort of overkill.

I know it's not required, but highly recommended, to keep <meta charset="utf-8"> in the index.php-document since the end users don't have access to modify my Apache server settings. But that only affects the client side content, and not the content fetched og created from the server side by PHP - if that made any sence...

EDIT : Here is my index.php:
The load.initialize.php requires my settings.cfg.php document at the very top...

<?php require_once 'initialize/load.initialize.php'; ?>
<!DOCTYPE html>
<head>
<meta charset="utf-8">
    ...
</head>
<body>
<!--
    this document includes page -header, -navigation, -content and -footer,
    where the content-part is just including other .php content pages or fetching content from database.
-->
<?php require 'template/load.template.php'; ?>
</body>
</html>

Solution

  • Your headers will already be passing the htaccess (if it's being triggered) and you don't need to define the charset again in PHP, but the html attribute is still good to keep.

    A good way to corfirm this is to check with a curl request from terminal or the like and see what the raw headers look like (a simple curl -v http://example.com/ will do). Check before and after and check for any differences to confirm.