Character encoding not declared in html document

Character encoding not declared in html document

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The indexmws.php Encoding Mystery

You are encountering a specific warning because your HTML document lacks an explicit character encoding declaration that modern browsers expect by default. The error message you received—*"The character encoding of the HTML document was not declared"*—is triggered when a browser attempts to render characters outside the US-ASCII range but cannot determine which code page (UTF-8, ISO-8859-1, etc.) to use for interpretation.

In your indexmws.php file, you have included a <meta> tag:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

Logically, this should resolve the issue. However, the presence of the warning indicates that the browser is ignoring this meta tag entirely. This behavior is almost exclusively caused by how PHP outputs data before it reaches your HTML header.

The Order of Operations in HTTP Headers

HTTP headers must be sent immediately after the server connects to the client. You cannot send any body content—including whitespace, newlines, or output generated by include statements—before the headers are finalized. If you emit even a single byte before sending the Content-Type header, the browser assumes the document is not UTF-8 unless explicitly told otherwise via the HTTP response header.

Your code begins with:

session_start();
if(!isset($_SESSION['validUser']) || $_SESSION['validUser'] !== true){
    header('Location: loginmws.php');
}

If $_SESSION['validUser'] is not set or is false, the script executes header('Location: ...'). This is a valid HTTP header. However, if that condition fails and execution continues to the include_once statements, you must ensure nothing has already flushed output.

The critical failure point often lies in the included files. You are including db.php, amazonmws.php, decidemws.php, and others. If any of these files contain:

  1. Whitespace before the opening <?php tag (e.g., blank lines at the top of the file).
  2. echo or print statements outside of conditional blocks.
  3. BOM (Byte Order Mark) characters saved in the file encoding.

Any of these factors will cause PHP to output a byte before your HTML header is generated. Once that happens, the browser discards the <meta> tag inside your HTML and relies solely on the HTTP Content-Type header. If your server configuration does not default to UTF-8, or if the included file outputs a BOM, the encoding becomes ambiguous, resulting in garbled text for non-ASCII characters.

Comparing the Working File vs. The Broken File

You noted that index.php works perfectly while indexmws.php fails, despite having identical HTML structures. The difference lies in the included files and the logic flow.

In index.php, you include:

include_once('db.php');
include_once('amazon.php');
include_once('decide.php');
include_once('author.php');

In indexmws.php, you include:

include_once('db.php');
include_once('amazonmws.php'); // Difference here
include_once('decidemws.php'); // Difference here
include_once('author.php');

The files named with the "mws" suffix might contain legacy code from an older project or a different framework. It is highly probable that one of these files (likely amazonmws.php or db.php if it's shared) has a Byte Order Mark (BOM) at the start of the file. Many text editors automatically add a BOM when saving as UTF-8 on Windows, but this BOM is invisible to humans and breaks strict HTML parsing in older browsers or specific configurations.

To verify this, open the included files in a hex editor. If you see EF BB BF at the very beginning of amazonmws.php, that is your culprit. The browser sees this byte before any headers are sent and assumes a different encoding or gets confused when your HTML declares UTF-8.

The Solution: Clean Includes and Header Configuration

To fix this, you must ensure that no output occurs before the first line of your PHP script executes.

  1. Check Included Files: Open every file listed in your include_once calls. Ensure there are no empty lines or spaces before <?php. If a file has a BOM, re-save it as "UTF-8 without BOM".
  2. Force the Header Explicitly: While not strictly necessary if output buffering is clean, you can force the correct HTTP header to override any ambiguity:
    <?php
    // Force UTF-8 before any other output
    header('Content-Type: text/html; charset=utf-8');
    
    session_start();
    if(!isset($_SESSION['validUser']) || $_SESSION['validUser'] !== true){
        header('Location: loginmws.php');
    }
    
    include_once('db.php');
    include_once('amazonmws.php');
    // ... other includes
    ?>
    
  3. Disable Output Buffering: If you are using a shared hosting environment or an older PHP setup, adding ob_implicit_flush(false); at the very top can help prevent accidental flushing of output buffers by included files.

By ensuring your included dependencies (db.php, amazonmws.php, etc.) do not emit any characters before the main script finishes its header logic, the <meta> tag and the subsequent HTML will be rendered correctly, eliminating the garbled text warning.

Our network