{"id":16658,"date":"2015-08-18T05:01:50","date_gmt":"2015-08-17T19:01:50","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=16658"},"modified":"2019-04-27T14:16:04","modified_gmt":"2019-04-27T04:16:04","slug":"php-content-type-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/php-content-type-primer-tutorial\/","title":{"rendered":"PHP Content Type Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php_content_type.php\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"PHP Content Type Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php_ct.jpg\" title=\"PHP Content Type Primer Tutorial\" \/><\/a><p class=\"wp-caption-text\">PHP Content Type Primer Tutorial<\/p><\/div>\n<p>Today we want to start a discussion on a big topic in web programming, the webpage content type (or <a target=_blank title='MIME information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/MIME'>mime type<\/a>).<\/p>\n<p>The normal association of webpages is with the &#8230;<\/p>\n<p><code><br \/>\ntext\/html<br \/>\n<\/code><\/p>\n<p> &#8230; mime type.  Server side languages such as PHP and ASP.Net, for the most part, create webpages in HTML format, but they don&#8217;t have to.  Supposing you have some PDF data then you can use PHP&#8217;s header method to use (the snippet from today&#8217;s PHP <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php_content_type.php_GETME\" title=\"php_content_type.php\">php_content_type.php<\/a>) &#8230;<\/p>\n<p><code><br \/>\n&lt;?php<br \/>\n\/\/ ...<br \/>\n\/\/ no echo statements anywhere up here<br \/>\n\/\/ ...<br \/>\n        <a target=_blank title='PHP header method information' href='http:\/\/php.net\/manual\/en\/function.header.php'>header<\/a>('Content-Type: ' . $ct);<br \/>\n        echo <a target=_blank title='PHP file_get_contents method information' href='http:\/\/php.net\/manual\/en\/function.file-get-contents.php'>file_get_contents<\/a>(<a target=_blank title='PHP urldecode method information' href='http:\/\/php.net\/manual\/en\/function.urldecode.php'>urldecode<\/a>($_POST['selname']));<br \/>\n?&gt;<br \/>\n<\/code><\/p>\n<p> &#8230; to show a webpage using a mime type defined by the PHP variable $ct which is &#8220;application\/pdf&#8221; in the URL &#8220;http:\/\/localhost:888\/logo.pdf&#8221; of our example <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php_ct.jpg\">tutorial picture<\/a>.<\/p>\n<p>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 &#8230;<\/p>\n<p><code><br \/>\n&lt;form target='_blank' action='.\/php_content_type.php' method='POST'&gt;<br \/>\n...<br \/>\n<\/code><\/p>\n<p> &#8230; the &#8220;target=&#8217;_blank'&#8221; of which is ensuring a new webpage will be opened and by saying &#8220;method=&#8217;POST'&#8221;, on submission, would POST (into $_POST[&#8216;selname&#8217;]) to itself (in a new webpage) the HTML input type=url as per &#8230;<\/p>\n<p><code><br \/>\nUrl: &lt;input <a target=_blank title='input type=url information from w3schools' href='http:\/\/www.w3schools.com\/html\/html_form_input_types.asp'>type='url'<\/a> name='selname' id='selname' style='width:70%;' value='HTTP:\/\/www.rjmprogramming.com.au\/MyBusinessUnidad\/Welcome_files\/logo.jpg'&gt;&lt;\/input&gt;<br \/>\n...<br \/>\n<\/code><\/p>\n<p> &#8230; the user defined URL of interest.<\/p>\n<p>In the second instance, or the <a target=_blank title='Callback information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/Callback_%28computer_programming%29'>callback<\/a> scenario, the <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php_content_type.php_GETME\" title=\"php_content_type.php\">php_content_type.php<\/a> code analyzes $_POST[&#8216;selname&#8217;] to derive a file extension (if none, would assume a mime type of text\/html).  That file extension we map against the very useful <a target=_blank title='Useful sitepoint webpage mime type list resource ... thanks heaps' href='http:\/\/www.sitepoint.com\/web-foundations\/mime-types-complete-list\/'>sitepoint<\/a> 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 &#8230;<\/p>\n<ol>\n<li>Navigate via Web Browser &#8230; up at the web browser address bar you&#8217;ll see the Url of your data source<\/li>\n<li>Show via PHP Content Type to New Webpage &#8230; up at the web browser address bar you&#8217;ll see the Url of php_content_type.php (which got POSTed to &#8230; so no $_SERVER[&#8216;QUERY_STRING&#8217;] even)<\/li>\n<li>Show PHP Content Type Below &#8230; ie. to an HTML <a target=_blank title='HTML iframe information from w3schools' href='http:\/\/www.w3schools.com\/tags\/tag_iframe.asp'>iframe<\/a>, the scenario of today&#8217;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php_ct.jpg\">tutorial picture<\/a><\/li>\n<\/ol>\n<p>With scenarios 2 and 3 above there is no hope that View-&gt;Page Source nor This Frame-&gt;View Page Source will result in a happy readable result, where scenario 1 will sometimes allow for this.<\/p>\n<p>With this knowledge in mind, or by trying a <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php_content_type.php\" title='Live run'>live run<\/a>, maybe now, some of the mystifying address bar URLs you encounter surfing the net may be more explainable to you &#8230; hope so.  Understanding of this topic can be a &#8220;light bulb&#8221; moment for many people, and a little bit of patience can reveal many possibilities with web applications, should you delve deeper yourself.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d16658' onclick='var dv=document.getElementById(\"d16658\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/?tag=text-widget\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d16658' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8230; text\/html &#8230; mime type. Server side languages such as PHP &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/php-content-type-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[1669,1667,576,587,792,1668,932,997,1319,1345,1404],"class_list":["post-16658","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-address-bar","tag-content-type","tag-html","tag-iframe","tag-mime","tag-mime-type","tag-php","tag-programming","tag-tutorial","tag-url","tag-web-browser"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/16658"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=16658"}],"version-history":[{"count":17,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/16658\/revisions"}],"predecessor-version":[{"id":44825,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/16658\/revisions\/44825"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=16658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=16658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=16658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}