Advertisement
illwieckz

client/server distance

Apr 10th, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.03 KB | None | 0 0
  1. function vincenty_distance($fromCoord, $toCoord) {
  2.         /* Vincenty formula for great-circle distance, in kilometers */
  3.         $earthRadius = 6371;
  4.         $fromLat = deg2rad($fromCoord["latitude"]);
  5.         $fromLon = deg2rad($fromCoord["longitude"]);
  6.         $toLat = deg2rad($toCoord["latitude"]);
  7.         $toLon = deg2rad($toCoord["longitude"]);
  8.         $deltaLon = $toLon - $fromLon;
  9.         $a = pow(cos($toLat) * sin($deltaLon), 2) + pow(cos($fromLat) * sin($toLat) - sin($fromLat) * cos($toLat) * cos($deltaLon), 2);
  10.         $b = sin($fromLat) * sin($toLat) + cos($fromLat) * cos($toLat) * cos($deltaLon);
  11.         $angle = atan2(sqrt($a), $b);
  12.         return $angle * $earthRadius;
  13. }
  14.  
  15. if ($_SERVER["HTTP_X_FORWARDED_FOR"] == null) {
  16.         $clientAddr = $_SERVER["REMOTE_ADDR"];
  17. }
  18. else {
  19.         $clientAddr = $_SERVER["HTTP_X_FORWARDED_FOR"];
  20. }
  21.  
  22. $serverCoord = geoip_record_by_name($serverAddr);
  23. $clientCoord = geoip_record_by_name($clientAddr);
  24. $client_distance = round(vincenty_distance($serverCoord, $clientCoord));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement