Added code to check if the user is within the radius for the GLink

php
Aadhavan Srinivasan 1 year ago
parent 22c85234da
commit 96abdd9852

@ -1,9 +1,63 @@
<?php <?php
$latitude = $_GET["latitude"];
$longitude = $_GET["longitude"]; // FUNCTION TO CALCULATE HAVERSINE DISTANCE GIVEN COORDINATES OF
// TWO POINTS, AND RADIUS (IN KM)
function haversine_distance($lat1, $long1, $lat2, $long2, $radius) {
$term_1 = $lat2 - $lat1;
$term_1 = $term_1 / 2;
$term_1 = sin($term_1);
$term_1 = pow($term_1, 2);
$term_2 = $long2 - $long1;
$term_2 = $term_2 / 2;
$term_2 = sin($term_2);
$term_2 = pow($term_2, 2);
$term_2 = $term_2 * cos($lat1);
$term_2 = $term_2 * cos($lat2);
$distance = $term_1 + $term_2;
$distance = sqrt($distance);
$distance = asin($distance);
$distance = $distance * 2;
$distance = $distance * $radius;
return $distance;
}
function km_to_miles($distance) {
return $distance * 0.62137119;
}
$user_lat = $_GET["latitude"];
$user_lat = $user_lat * (M_PI / 180);
$user_long = $_GET["longitude"];
$user_long = $user_long * (M_PI / 180);
$glink = $_GET["glink"]; $glink = $_GET["glink"];
printf("Lat is %s, Long is %s, and glink is %s",$latitude,$longitude,$glink); $cluster = Cassandra::cluster()->withPersistentSessions(true)->build();
$keyspace = 'glink';
$session = $cluster->connect($keyspace);
$statement = $session->prepare('SELECT latitude,longitude,radius,url FROM data WHERE shortlink=? ALLOW FILTERING;');
$result = $session->execute($statement,array('arguments' => array($glink)));
$link_lat = floatval($result[0]['latitude']);
$link_lat = $link_lat * (M_PI / 180);
$link_long = floatval($result[0]['longitude']);
$link_long = $link_long * (M_PI / 180);
$target_radius = intval($result[0]['radius']);
$url = $result[0]['url'];
$distance = haversine_distance($link_lat, $link_long, $user_lat, $user_long, 6371.009);
$distance = km_to_miles($distance);
if ($distance <= $target_radius) {
printf("%s",$url);
} else {
printf("B");
}
// Check the database to see if user is allowed to access the URL. If they are, respond 'Yes' (for the time being), if they are not, respond 'No' (for the time being). // Check the database to see if user is allowed to access the URL. If they are, respond 'Yes' (for the time being), if they are not, respond 'No' (for the time being).

Loading…
Cancel
Save