Thread: IP Based Image

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    198
    Rep Power
    4

    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...
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    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.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Oxford, United Kingdom
    Posts
    40
    Rep Power
    2
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    198
    Rep Power
    4
    Hi NavNav, thanks for your reply... can you elaborate on what you mean, thanks in advance
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Oxford, United Kingdom
    Posts
    40
    Rep Power
    2
    Originally Posted by oo7ml
    Hi NavNav, thanks for your reply... can you elaborate on what you mean, thanks in advance
    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).

    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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    198
    Rep Power
    4
    Ok cool, thanks, that makes perfect sense

IMN logo majestic logo threadwatch logo seochat tools logo