On this day ... one PHP script with three uses
What 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 .
(written 2011-09-26, updated 2011-09-27)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H307 - PHP - Web2 and caching [1633] Changing a screen saver from a web page (PHP, Perl, OSX) - (2008-05-06)
[1647] Exchange Rates - PHP with your prices in your users currency - (2008-05-19)
[1733] memcached - overview, installation, example of use in PHP - (2008-08-02)
[1812] Starting Ajax - easy example of browser calling up server data - (2008-09-27)
[1813] Ajax - going Asyncronous and what it means - (2008-09-28)
[1814] Javascript/HTML example, dynamic server monitor - (2008-09-28)
[1926] Flash (client) to PHP (server) - example - (2008-12-06)
[1995] Automated server heartbeat and health check - (2009-01-16)
[2196] New Example - cacheing results in PHP for faster loading - (2009-05-24)
[2321] Uploading and Downloading files - changing names (Perl and PHP) - (2009-08-04)
[2545] Scraping content for your own page via PHP - (2009-12-21)
[3029] PHP data sources - other web servers, large data flows, and the client (browser) - (2010-11-04)
[3094] Setting your user_agent in PHP - telling back servers who you are - (2010-12-18)
[3186] How to add a customised twitter feed to your site - (2011-02-27)
[3955] Building up from a small PHP setup to an enterprise one - (2012-12-16)
[3999] Handling failures / absences of your backend server nicely - (2013-02-08)
[4055] Using web services to access you data - JSON and RESTful services - (2013-03-29)
[4075] Further recent PHP examples - (2013-04-28)
[4106] Web server efficiency - saving repetition through caches - (2013-05-30)
[4136] How do I post automatically from a PHP script to my Twitter account? - (2013-07-10)
[4627] Caching results in an object for efficiency - avoiding re-calculation - (2016-01-20)
Some other Articles
Hangers, luggage and possessionsFrom flowchart to program - code design for the newcomerFlying Colours!Catching the fishes first?On this day ... one PHP script with three usesAway to train - but still around by video for Melksham meetingsStepping stones - early coding, and writing re-usable code quicklyMySQL, MySQLi, PDO or something else - how best to talk to databases from PHPYour PHP website - how to factor and refactor to reduce growing painsReading and using emails including enclosures on your web server.