-2

I need to display a different home page in US compared to the one designed for Europe (especially UK). Is this possible to achieve in PHP?

The PHP website that I have developed uses JS, Ajax and Bootstrap extensively.

Siguza
  • 21,155
  • 6
  • 52
  • 89
user4826347
  • 783
  • 2
  • 11
  • 29
  • Yep sure thing it is, look for any open source ip database and those are fairly reliable, not 100% accurate – Hanky Panky Jul 08 '15 at 17:17
  • 1
    You need a geolocation database. – angelcool.net Jul 08 '15 at 17:18
  • Look at MaxMind, who do a free product to work out your country from your IP. Always offer an override, for users who use proxies and VPNs. Based on the answer you get, you can redirect to a different page. – halfer Jul 08 '15 at 17:22

2 Answers2

2

If the primary goal here is to cater to specific demographic groups, rather than trying to implement some sort of location-based lock-out mechanism, then instead of using a geo location database as primary guide, consider using the client's browser locale (c.f. Detect Browser Language in PHP).

Maxmind offers a neat (free) IP-based location look-up database available here: https://dev.maxmind.com/geoip/legacy/geolite/

0
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
echo $details->city;

e.g. for ip 8.8.8.8 it gives

{
  "ip": "8.8.8.8",
  "hostname": "google-public-dns-a.google.com",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "loc": "37.3860,-122.0838",
  "org": "AS15169 Google Inc.",
  "postal": "94040"
}
pinkal vansia
  • 10,240
  • 5
  • 49
  • 62
  • Keep in mind that this suggested solution (while correct) will perform an additional GET request which may slow down the UX. Also, the solution depends on `ipinfo.io` being available. – Christian Kiewiet Jul 08 '15 at 17:23
  • This seems to be an easy way, I bet. I am a bit concerned on being dependent on a third party service though! – user4826347 Jul 08 '15 at 17:43