
|
|
|||
Effective Geotargeting with PHPBy Akash Mehta2008-01-27
Let's write some code! Once you've got the library and the database — throw them all into the same folder for simplicity — it's actually pretty easy. Load up your favourite text editor and type this in: <?php This presumes you've put both the geoip.inc library and geoip.dat database in the same folder. $_SERVER['REMOTE_ADDR'] should represent your IP address. If you're behind a proxy or firewall this could be somewhat inaccurate and possibly even a little misleading, but generally this should work fine. You won't get much luck running this on your local machine, as your IP would be a wonderfully non-descriptive 127.0.0.1; you'll have to upload it to a remote server to see the results. You should see a very short country code; a couple of letters, nothing else. For example, here's my result: AU That may not look like much, but it's not just any country code: it's your country code. In four lines of code we've worked out a standard country code corresponding to your location — and imagine the possibilities with that! Of course, there's much more we can do. From here, experiment with serving special content to particular countries and having friends abroad try to visit the page. A touch of PEARWe're all for code re-use here, so let's take a look at the PEAR version. Just a brief intro: PEAR is a repository of PHP libraries all built in approximately the same somewhat predictable manner. They are designed to be highly reusable, making them fantastic for completing projects in short timeframes, and help you avoid reinventing the wheel. But PEAR isn't just about libraries; it's a fully-fledged package distribution system that probably came installed with your version of PHP, as well as a community creating and supporting all this great work. See pear.php.net for more info. The PEAR version works in much the same way, only has improved PHP 5 style error handling and is a little more clear. Here's how to do it with the PEAR library: <?php Notice here we can natively catch any errors that occur; in the older version, we had to test if no value was returned. The PEAR version is not necessarily more reliable at conducting lookups — it's using the same database — but it will be a little easier to work with if you've used PEAR packages before, and it also makes use of some useful PHP 5 features. I recommend using the PEAR version, but it's up to you and for basic use won't make much of a difference. For high traffic sites with basic geolocation needs, you might find that the original version offers better performance as it doesn't come with the overhead of an object. Performance considerationsLet us take a look at our original code snippet: <?php All very well; nice and simple. But is it really? What's going on behind the scenes with VisualsOf course, this country code isn't of much use on its own anyway — so how about we make it a little more useful to your users? Show them a small version of their country's flag to invoke the patriot inside anyone! From Mark James, of Silk icon set fame, comes a fantastic library of 247 flags for just about every country out there. What's more, they're small, totally free, look good on any web page and follow the same naming conventions as your PHP routine. Just drop them in place, lowercase your country codes and show the appropriate flag to easily impress any user. A complete geotargeting systemGiven these performance considerations and the visuals possible with the flag icons, let's create a simple geotargeting system that you can drop in to your website. First, we initialise: <?php Note that including our geoip.inc library is in itself overhead we would like to avoid where performance is an issue. As a result, we deal with it later instead. We need our script to save the user's country in the session and show a nice little PNG of the country's flag. So, we first test if the session data isn't there, and set it accordingly using our script from above: if (!isset($_SESSION['geocountry'])) {
On subsequent page loads, the 'geocountry' session variable would already have been set by this snippet; it essentially serves as a user-based cache, and we leave the tricky session handling to PHP (although you may want to use your own session library as part of a framework). Next, we display the PNG flag: $country = strtolower($_SESSION['geocountry']); Now, load the script up in your web browser. With any luck you should see something like the following:
It may seem rather small, but it's actually the perfect size for fitting into a content-rich web page, and works quite nicely inside navigation bars and menu sections of any size. It's also very recognisable to the user as their flag, and finally, after all the pretty visuals are dealt with, we now have the user's country available for use in our session's 'geocountry' variable. Tutorial Pages: » Introduction » IP Targeting: It’s tricky business! » Getting started » Let's write some code! » Tips and tricks » Going further |
||||
| About the NetVisits, Inc Network | Write For Us | Advertise Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy. |
Visit other NetVisits, Inc. sites: |