Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Character encoding not declared in html document

Tags:

php

encoding

I have a file that I am getting a very weird error on. The error is:

The character encoding of the HTML document was not declared. 
The document will render with garbled text in some browser configurations if 
the document contains characters from outside the US-ASCII range. 
The character encoding of the page must to be declared in the document or 
in the transfer protocol.

the file this comes from is (indexmws.php):

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

include_once('db.php');
include_once('amazonmws.php');
include_once('decidemws.php');
include_once('author.php');
include_once('amazonPricingMWS.php');

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Decision Maker</title>

This is an exact duplicate of a file that does not throw the error (index.php)with the exception of adding amazonPricingMWS.php and redirecting to pages with mws.php in the title:

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

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

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Decision Maker</title>

can anyone explain to me why I am getting this error in indexmws.php?

like image 451
Jim Avatar asked Jul 24 '12 14:07

Jim


People also ask

How do you declare character encoding in HTML?

Quick answer. Always declare the encoding of your document using a meta element with a charset attribute, or using the http-equiv and content attributes (called a pragma directive).

What is character encoding not declared?

What Does “The Character Encoding Was Not Declared” Mean? A JavaScript error saying that “the character encoding of the HTML document was not declared” indicates that your page might be displaying garbled text.

How do I add UTF-8 in HTML?

UTF-8 is the most most common character encoding on the web. You can set UTF-8 encoding and other formats using the <meta charset> tag in HTML5.

How do I encode HTML files?

Load the data to HTML–encode from a file, then press the 'Encode' button: Browse: Alternatively, type or paste in the text you want to HTML–encode, then press the 'Encode' button.


2 Answers

The error is coming because the browsers expect the encoding format in the first 1024 bytes of the file. It may be the case that there is some content being outputted by the included files in the first case.

The browsers now buffer the first 1024 bytes of the file to check for the character encoding. If the encoding description is not encountered in the first 1024 bytes, this warning is displayed.

In your case, you can use a php header for specifying the content type before the other files are included:

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

For more information, read this: http://gtmetrix.com/specify-a-character-set-early.html

like image 110
Karan Punamiya Avatar answered Oct 08 '22 11:10

Karan Punamiya


I had similar problem But teh exact reason behind this was that I missed following to add

<meta content="utf-8" http-equiv="encoding">

in head tag after

<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
like image 37
Krishan Gopal Avatar answered Oct 08 '22 11:10

Krishan Gopal