HTTP Redirects in PHP

Here’s a really quick and simple example of an HTTP redirect using PHP.

The British Geological Survey publishes live data from its seismographs, located in many parts of the UK. The URL for the latest data changes daily, and is derived from the calendar. So it’s easy to reconstruct the URL. But I’d like to access the current day’s data for my local area from an unchanging, static URL.

Therefore, the static URL needs to create an HTTP redirect to the current day’s data from the location I want to use, my local seismograph at the former Royal Observatory at Herstmonceaux, Sussex. Here is the code to do that. The method ought to be self-explanatory. To be precise, this code causes the Apache webserver to instruct a client (your web browser) to follow a 302 Redirect.

You can give this page of PHP any name you like. My URL is http://johnwarburton.net/did-the-earth-move.php

$DIRECTORY = "http://www.earthquakes.bgs.ac.uk/helicorder/heli_dir_shz/";
$PREFIX    = "HMNX_SHZ_GB_00.";
$timestamp = date("Ymd");
$SUFFIX    = "00.gif";

$redirectUrl = $DIRECTORY.$PREFIX.$timestamp.$SUFFIX;

header("Location: ".$redirectUrl);
exit;

Leave a Reply

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