简体   繁体   中英

Do I need to declare content type/charset in both HTML and PHP?

If the content type and character set are declared in the PHP header, is there a reason to have them again in the usual HTML DTD?

 <?php ob_start( 'ob_gzhandler' ); header('Content-type: text/html; charset=utf-8'); ?> // here <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> // and here ...

If you are sending the charset in the headers, the is no need to repeat it in the HTML markup.

It is better to send this information in one place (DRY principle), as if the charsets conflict (ie. a header with UTF-8 and a meta with iso-8859-1), the browser will probably go to quirks mode.

Having said that, some automated tools (web scrapers) may not look at the header and deduce the page encoding only by the meta tag.

It is important to keep both the header and meta tag the same for each page - mixing different charsets may confuse browsers and cause display issues.

Having the charset in the HTML source may be helpful if someone decides to save a page, or for web scrapers :). libxml looks up the meta tag to determine the charset to use when parsing the markup. Show your fellow developers some web scraping love.

If you declare it in the HTTP headers, then it will survive transcoding by proxies and won't ever trigger a "Whoops, I guessed the wrong encoding, restart parsing from top" situation in browsers.

If you declare it in the body of the document then it will survive being access outside of HTTP (or another system with content-type headers, such as email).

If you declare it in both then you get the best of both worlds so long as no transcoding happens.

Note that if you don't use UTF-8 or UTF-16 then the XML spec requires that you specify it in the XML prolog (and that using an XML prolog will trigger Quirks mode in IE6).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM