Setting Document Type PHP

By default, PHP sends an HTTP header specifying the document as being HTML. The Content-Type header specifies the MIME type text/html, and the browser interprets the code as HTML. Sometimes you will wish to create other types of documents with PHP. MIME types are administered by IANA, the Internet Assigned Numbers Authority.

At times, you may wish to take advantage of how browsers react to different types of content. For example, text/plain displays in a fixed-width font with no interpretation of HTML. If you use */* for the content type, the browser displays a dialog window for saving the file. Perhaps the most interesting use is for launching a helper application. This creates a tab-delimited text file that may launch Microsoft Excel. Take note that the computer must meet a few qualifications, however. First, it probably needs to be running Windows, and it must have Microsoft Excel installed. Newer versions of Excel associate the application/ content type with .xls files.

My experience has been that these headers will cause an Excel OLE container insideeither MSIE or Netscape Navigator on a Windows machine, but your mileage may vary.Other browsers will likely ask the user if the file should be saved. Notice the second header, Content-Disposition. This is not part of the HTTP 1.1 standard but is widely implemented. It allows you to suggest a file name. If you add attachment; to the header, the browser may choose to open Excel in a separate window.

Using Content-Type this way is almost black magic, since browsers don't follow a standard when encountering different MIME types. This technique has proven to be most successful for me when writing intranet applications where I had the luxury of serving a narrow set of browsers.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics