December 27th, 2012, 10:42 AM
IP Based Image
Hi, i have a banner area on my website where i would like to display an image. I would like to display a different image in the banner area, depending on a users' location.
I have 4 different images:
01 - usa_banner.jpg (default)
02 - europe_banner.jpg
03 - uk_banner.jpg
04 - australia_banner.jpg
If a user views the site from a country outside of the top 4 areas, then i would like to display the default usa_banner.jpg image.
I have created my site using codeigniter. What is the best way to work out the IP ranges etc. Any advice on this would be greatly appreciated. Thanks in advance...
December 27th, 2012, 12:30 PM
your friend Google will tell you everything you need to know. Just search for "ip location" or "geolocation".
PHP even has an extension for this, but I don't know how it works and how good it is.
December 27th, 2012, 01:35 PM
If you do decide to use a 'IP To Location' API, don't do it during page requests. If for whatever reason the API fails to respond, you'll be keeping your users waiting. And that's not cool.
Do it through AJAX. Perform the page request as normal and then have JS/JQuery make an AJAX request to your server, where your server talks to the API. Or even better, if the API supports JSONP and does not require any kind of auth, do it all through the client and the API. Meaning, your server can kick back and do nothing xD
Either way, I would advise not to perform an API requests during a page request. You could end up annoying a lot of users without even realising.
December 27th, 2012, 02:59 PM
Hi NavNav, thanks for your reply... can you elaborate on what you mean, thanks in advance
December 27th, 2012, 05:47 PM
Well, an 'IP To location' API is just a small web service that has access to a large DB of IP addresses and their geo location data (country, city, ect ect).
Originally Posted by oo7ml
Developers can use the API by simply passing an IP address to it, and having the API return the geo data of that IP.
So we can take our visitors IP, pass it to the API and it will return the geo data for that IP (meaning, we will know where the user is based (the country and city).
There's loads of them out there. Most are free, but some may charge.
What I was saying in my previous post was that if you do use this method to trace a user by IP, don't do it during page requests.
When using a web service API, you're making HTTP requests and you're waiting for those requests to come back with a response. If the API you're requesting is running slow for whatever reason, your HTTP request will take a while to complete. So, if you make the HTTP request (talk to the API) during a page request and the API is having a bad day (running rather slow), your user will be kept waiting as the page request will not proceed until the HTTP request is complete. So you're basically having your site/app's performance depend on that API.
So, that's why you shouldn't do it. Instead, do not talk to the API during page requests but do it after the page request is complete, with JS and AJAX.
Simply have your page(s) make AJAX requests to your server from the client side (users machine). It will request a script on your server which then talks to the API (makes the appropriate HTTP requests) and returns the response. Then you can have JS change/load the specific image, based on the response.
December 28th, 2012, 10:04 AM
Ok cool, thanks, that makes perfect sense