0

I have been looking around Google and SO for a way to get the GPS location of a user using a mobile phone with PHP. However, the only solutions I have found use Javascript, and I would rather not have to do some complicated stuff using Javascript when I can just add some PHP code to my already existing system.

My question is simple: How can I get the GPS location of somebody browsing my site with PHP, instead of Javascript?

Additionally: If I have to use Javascript, how can I access the value from PHP? Also, is there any way to get a city/country from this GPS location, or possibly another method to get the city/country?

tomb
  • 1,817
  • 4
  • 21
  • 40

2 Answers2

4

You can't get the GPS location using PHP.

You could use GeoLocation to approximate it (using the IP address), but this is notoriously unreliable and best avoided.

The other option is to use the location API built into HTML5. Upon visiting your site, those using a capable mobile device(or browser) will be asked if they wish to disclose their location for functional reasons, but you can't force users to divulge it:

function show_map(position) {
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;
  // let's show a map or do something interesting!
}

An example guide/overview worth reading is here (which includes a live example of how location-aware browsing works).

Location information is a very sensitive area with regard to privacy and thus handset manufacturers won't allow automatic disclosure of the information you're looking for.

Further reading resources:

Community
  • 1
  • 1
nickhar
  • 19,981
  • 12
  • 60
  • 73
  • How can I pass this location to a PHP script? Also, how can I get an actual location (such as a city, or country) from this? – tomb Apr 05 '13 at 11:32
3

There is no way to retrive this data via PHP, remember that PHP is executed in the server, not in the client, so you need a client execution language like javascript.

m4t1t0
  • 5,669
  • 3
  • 22
  • 30
  • However, is any GPS value sent through in the HTTP request? If not, how would I be able to pass the value from Javascript to PHP? – tomb Apr 05 '13 at 10:17
  • you can pass values between javascript and PHP via AJAX – m4t1t0 Apr 05 '13 at 10:18
  • How can I get the Javascript value from the PHP script though? I can see how I could get a PHP value to Javascript, but how can I do it the other way around? – tomb Apr 05 '13 at 10:19
  • You need to do an ajax call and then store it in a dabase or whatever you want. – m4t1t0 Apr 05 '13 at 10:21