PHP Content Type Primer Tutorial

PHP Content Type Primer Tutorial

PHP Content Type Primer Tutorial

Today we want to start a discussion on a big topic in web programming, the webpage content type (or mime type).

The normal association of webpages is with the …


text/html

… mime type. Server side languages such as PHP and ASP.Net, for the most part, create webpages in HTML format, but they don’t have to. Supposing you have some PDF data then you can use PHP’s header method to use (the snippet from today’s PHP php_content_type.php) …


<?php
// ...
// no echo statements anywhere up here
// ...
header('Content-Type: ' . $ct);
echo file_get_contents(urldecode($_POST['selname']));
?>

… to show a webpage using a mime type defined by the PHP variable $ct which is “application/pdf” in the URL “http://localhost:888/logo.pdf” of our example tutorial picture.

We show this distinction today by writing a PHP web application that in the first instance is a web page consisting of an HTML form as per …


<form target='_blank' action='./php_content_type.php' method='POST'>
...

… the “target=’_blank'” of which is ensuring a new webpage will be opened and by saying “method=’POST'”, on submission, would POST (into $_POST[‘selname’]) to itself (in a new webpage) the HTML input type=url as per …


Url: <input type='url' name='selname' id='selname' style='width:70%;' value='HTTP://www.rjmprogramming.com.au/MyBusinessUnidad/Welcome_files/logo.jpg'></input>
...

… the user defined URL of interest.

In the second instance, or the callback scenario, the php_content_type.php code analyzes $_POST[‘selname’] to derive a file extension (if none, would assume a mime type of text/html). That file extension we map against the very useful sitepoint resource we thank very much to arrive at a suitable mime type to be suitable for that PHP $ct variable we talked about earlier. This gives us the scenario used for the middle of three buttons …

  1. Navigate via Web Browser … up at the web browser address bar you’ll see the Url of your data source
  2. Show via PHP Content Type to New Webpage … up at the web browser address bar you’ll see the Url of php_content_type.php (which got POSTed to … so no $_SERVER[‘QUERY_STRING’] even)
  3. Show PHP Content Type Below … ie. to an HTML iframe, the scenario of today’s tutorial picture

With scenarios 2 and 3 above there is no hope that View->Page Source nor This Frame->View Page Source will result in a happy readable result, where scenario 1 will sometimes allow for this.

With this knowledge in mind, or by trying a live run, maybe now, some of the mystifying address bar URLs you encounter surfing the net may be more explainable to you … hope so. Understanding of this topic can be a “light bulb” moment for many people, and a little bit of patience can reveal many possibilities with web applications, should you delve deeper yourself.

If this was interesting you may be interested in this too.

This entry was posted in eLearning, Tutorials and tagged , , , , , , , , , , . Bookmark the permalink.

20 Responses to PHP Content Type Primer Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *