0

I am developing web application and i need to know the location of the user they connecting from

For example this website www.craigslist.org it can detect my location and redirect me to sub domain So, it's redirect to:

cairo.craigslist.org

boston.craigslist.org/

What is the best way to do that with PHP ?

PART TWO

How can i make it redirect to subdomain with country/city ? like the above example ?

Sameh Serag
  • 747
  • 1
  • 8
  • 22

1 Answers1

1

In pure PHP without a 3rd party service, take a look at the GeoIP extension library: http://php.net/manual/en/book.geoip.php

You'll be highly limited using this however to country, region, and ISP. I think the State and City longitude and latitude starts getting less accurate.

You could also check out https://github.com/maxmind/GeoIP2-php which seems to be a library intended to replace GeoIP.

Ray
  • 40,256
  • 21
  • 101
  • 138
  • 2
    link only answers are discuraged –  Apr 03 '16 at 20:19
  • @Ray thanks for your answers ... how can i make it redirect to subdomain ? – Sameh Serag Apr 03 '16 at 20:35
  • @Marc J After obtaining the location (lets call it `$location`), you could use a simple location-header for redirecting: `header('Location: http://'.$location.'.example.com/');` – A. Fink Apr 03 '16 at 20:45
  • @A.Fink ... shall i create subdomains with all countries/cities ?!! – Sameh Serag Apr 03 '16 at 20:49
  • @MarcJ You could do, but i think this is not useful. If you make location less accourate, like continent or maybe country (would still be much), it could be useful. – A. Fink Apr 03 '16 at 20:54