On this day ... one PHP script with three uses
Archive - Originally posted on "The Horse's Mouth" - 2011-09-26 23:22:12 - Graham EllisWhat happened on this day in previous years? What events in history, and what was the subject of this blog? As part of a demonstration (of caching), I wrote an example script that constructed a report ... but then I wanted to use it in various ways. As a demonstration page in its own right. As a text data feed available for use elsewhere. And as a file that can be included within other files.
In PHP, the header function lets you reset the MIME type that your script returns - the file extension you use only sets the default and that can be easily overridden.
Here's the code at the end of the demonstration which selects what to return, based on request parameters in the URL:
if (! $_REQUEST[silent]) {
if ($_REQUEST[feed]) {
header("Content-type: text/plain");
print ($tout);
} else {
?>
<html>
<head>
<title>On this day ...</title>
</head>
<body>
<h1>On this day ... what was I blogging and what happened
in recent years?</h1>
<table cellpadding="4" border="0"><?= $hout ?></table>
</body>
</html>
<?php }} ?>
Here are examples of the three calls used:
http://www.wellho.net/demo/onthisday.php [try]
http://www.wellho.net/demo/onthisday.php?feed=100 [try]
http://www.wellho.net/demo/onthisday.php?feed=100&silent=1 (No link - empty page!)
The final URL is an example of how the same page can be set up for use within another page on your website, via a PHP require or include. I have used it to generate data fo fill in our standard template for an "on this day" page which you can visit [here] via the URL http://www.wellho.net/net/onthisday.html .