0

I'm looking for a way to determine what is the hemisphere of a user who is visiting a web page by using PHP code.

This question is similar to this JS question but using PHP.

user502052
  • 14,803
  • 30
  • 109
  • 188

1 Answers1

0

Based on this answer: https://stackoverflow.com/a/17864552/1427345

$ip = $_SERVER['REMOTE_ADDR'];

$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));

list($lat, $long) = explode(',', $details->loc);

if ($lat > 0)
    echo 'Northern Hemisphere';
else
    echo 'Southern Hemisphere';
Jacob Mulquin
  • 3,458
  • 1
  • 19
  • 22
  • Is it possible to get the hemisphere info without using third party services? – user502052 Apr 17 '22 at 20:42
  • Yes you would need to download a free GeoIP database and then reference that instead of calling the 3rd party service. – Jacob Mulquin Apr 17 '22 at 20:43
  • And is it possible to get the hemisphere info simply based on the client data sent along with his/her browser e.g. datetime, etc.? – user502052 Apr 17 '22 at 20:46
  • I don't believe so, in PHP we have the variable `$_SERVER['REQUEST_TIME']`, but it is the time on the server. In HTTP connections, the client does not send it's timestamp to the server. – Jacob Mulquin Apr 17 '22 at 20:50
  • You could combine JS and PHP, and pass the client's time through an AJAX request, but then you may as well do it all in JS. – Jacob Mulquin Apr 17 '22 at 20:52
  • OK. The JS code in the [linked question](https://stackoverflow.com/a/65658594/502052) could be correct to get the hemisphere info? – user502052 Apr 17 '22 at 20:56
  • Give it a go. I don't know for sure as I don't have a VM with a GUI in the northern hemisphere to test. – Jacob Mulquin Apr 17 '22 at 20:58