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

  1. Someone in my Facebook group shared this site with us so I came to take a look. I’m definitely enjoying the information. I’m bookmarking and will be tweeting this to my followers! Fantastic blog and terrific design

  2. Thanks a bunch for sharing this with all people you actually recognise what you’re speaking approximately! Bookmarked. Kindly also consult with my website =). We could have a hyperlink trade agreement between us!

  3. you’re actually a just right webmaster. The site loading pace is amazing. It seems that you’re doing any distinctive trick. Furthermore, The contents are masterwork. you have performed a excellent job on this matter!

  4. Useful info. Fortunate me I discovered your website accidentally, and I’m surprised why this accident did not came about in advance! I bookmarked it.

  5. There is clearly a lot to realize about this. I feel you made some nice points in features also.

  6. whoah this weblog is wonderful i love studying your posts. Keep up the great paintings! You realize, lots of people are searching round for this information, you could help them greatly.

  7. I truly wanted to jot down a quick message in order to appreciate you for all of the fabulous recommendations you are writing at this website. My particularly long internet research has at the end been compensated with good ideas to talk about with my colleagues. I ‘d suppose that most of us visitors actually are very much fortunate to be in a great community with many brilliant individuals with interesting concepts. I feel very much blessed to have encountered your entire web page and look forward to really more fun times reading here. Thank you once again for a lot of things.

  8. My wife and i were relieved Emmanuel managed to conclude his inquiry out of the ideas he discovered when using the weblog. It’s not at all simplistic to just choose to be giving freely things which the rest have been making money from. And we keep in mind we now have the writer to be grateful to for this. All the explanations you’ve made, the straightforward blog menu, the friendships you will help to instill – it is all powerful, and it’s helping our son in addition to our family believe that the theme is brilliant, and that’s quite fundamental. Many thanks for all!

  9. Just wish to say your article is as astounding. The clearness in your post is just nice and i can assume you are an expert on this subject. Fine with your permission let me to grab your RSS feed to keep up to date with forthcoming post. Thanks a million and please continue the gratifying work.

  10. I want to express appreciation to this writer just for rescuing me from this type of dilemma. Just after surfing through the world wide web and finding basics that were not beneficial, I was thinking my entire life was gone. Living minus the approaches to the difficulties you have fixed by way of your entire post is a serious case, as well as those that would have adversely damaged my entire career if I had not noticed your web page. Your actual knowledge and kindness in dealing with all areas was priceless. I don’t know what I would have done if I had not come across such a thing like this. I am able to at this point look ahead to my future. Thank you so much for your high quality and effective help. I won’t be reluctant to propose your web sites to any individual who needs and wants assistance about this subject matter.

  11. I’ve been browsing online vineoris more than three hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my view, if all web owners and bloggers made good content as you did, the web will be a lot more useful than ever before.

  12. abundant says:

    I enjoy you stating your perspective.. My internet browsings seem total.. thanks. So content to have identified this post.. So happy to have located this publish..

  13. Simply wish to say your article is as surprising. The clarity in your post is just cool and i can assume you are an expert on this subject. Fine with your permission let me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please continue the rewarding work.

  14. Expert says:

    Particularly useful viewpoint, many thanks for sharing with us.. Fantastic feelings you possess here.. So happy to possess located this article.. My internet searching seem full.. thank you.

  15. hey there and thank you for your info – I’ve certainly picked up something new from right here. I did however expertise some technical points using this site, as I experienced to reload the site many times previous to I could get it to load correctly. I had been wondering if your web hosting is OK? Not that I’m complaining, but sluggish loading instances times will often affect your placement in google and can damage your quality score if advertising and marketing with Adwords. Anyway I’m adding this RSS to my e-mail and can look out for much more of your respective exciting content. Ensure that you update this again soon..

  16. amazing says:

    So pleased to get found this publish.. So happy to possess found this publish.. My personal internet searching seem complete.. thank you. My personal internet searching seem total.. thank you.

  17. generous says:

    indeed, research is having to pay off. Respect the admission you offered.. Treasure the posting you provided.. My browsing efforts seem total.. thank you.

  18. Vital says:

    I am constantly invstigating online for tips that can help me. Thank you!

  19. Micah says:

    Aw, this was an extremely nice post. Spending some time and actual effort to make a good article… but what can I say… I procrastinate a whole lot and don’t seem to get nearly anything done.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>