/    Sign up×
Community /Pin to ProfileBookmark

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…

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@firesnakerDec 27.2012 — I searched your query on Google and found a great Stack Overflow thread. In summary here is the solutions:

You can use APIs:

Hostinfo API:

http://www.hostip.info/use.html

Or you can use a local database on your server, it does need to be updated regularly though.

http://phpweby.com/software/ip2country

If you have money, you can use the Maxmind GeoAPI built-in to your PHP server.

http://php.net/manual/en/book.geoip.php

You want to check the original stack overflow thread here http://stackoverflow.com/questions/3650006/get-country-of-ip-address-with-php
Copy linkTweet thisAlerts:
@eval_BadCode_Dec 27.2012 — You shouldn't use an API when you already have existing data on your machine to do this.

/usr/share/GeoIP/GeoIP.dat

you can use apache2's mod_geoip (apt-get install apache2-mod-geoip or yum install mod_geoip.x86_64)

then, restart apache2. and now

nano /var/www/html/geoiptest.php
[code=php]
<html>
<body>
<?php
$country_name = apache_note("GEOIP_COUNTRY_NAME");
print "Country: " . $country_name;
?>
</body>
</html>
[/code]


It worked like a charm for me.

Country: United States
×

Success!

Help @oo7ml spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...