Main Content

Redirecting to your main domain for correct security keys

Archive - Originally posted on "The Horse's Mouth" - 2010-03-13 04:03:39 - Graham Ellis

Do you want to come to Well House Manor as a base to see Wiltshire or for a course? If it's you're first visit you'll want to find us on a map - and we have a map at http://www.wellho.net/demo/gmap.php which you can zoome to five levels at the click of a button and do other smart things with. And that map is Google powered.

But not everyone arrives at that page via www.wellho.net; you'll find that it's mirrored on www.wellho.co.uk, for example, and a handful of other domains. And occasionally we find people arriving by IP address. And if they did, they got not a map but an error message about the Google key being wrong.

Rather than register and maintain a series of keys, I have modified the map page to redirect visitors to the www.wellho.net site if they've arrived from elsewhere, by checking which server they've called up within my PHP and sending out a short redirect page if they're on a subsidiary domain. The extra code is as follows:


if ($_SERVER[SERVER_NAME] != "www.wellho.net") {
 
print <<<DONE
<html>
<head>
<meta http-equiv="refresh" content="6;url=http://www.wellho.net/demo/gmap.php">
</head>
<body>
This demonstration relies on a Google key on our main server (www.wellho.net)
but you are visiting from a different domain. You will be redirected shortly
(if you are not, please follow
<a href=http://www.wellho.net/demo/gmap.php>this link)</a>.
</body>
</html>
DONE;
 
exit(0);
}



Literally, that's saying "if you are not on the main server, please move on to that server after showing this page for 6 seconds". This is known as client pull - it relies on the browser to request the new page - so it's good practise to provide an onwards link too, just in case the browser can't / won't do as it has been directed.

Try out this, and the map - here are links to the page that redirects, and to the working page. If you're interested in the map iteself and how we generate that, you can see our source code here, and we'll teach you how to write code like this on our PHP techiques Workshop and Intermediate PHP training weekend.




Index under "This web site needs a different Google Maps API key. A new key can be generated at http://code.google.com/apis/maps."