0

I have a script which calculates the distance between 2 locals, and I want to insert the distance variable in to the cookie for later use.

The script works just fine but when it should set the cookie nothing happens.

This is my current code - in my local server it works fine but not in prodution server. There is no difference in my code on the 2 servers.

<?php


if (isset($_GET['submit'])) {

$origin = $_GET['origin'];
$destination = $_GET['destination'];
/* In imperial unit
$distance_data = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins='.urlencode($origin).'&destinations='.urlencode($destination).'&key=AIzaSyBg8mazOCFsHlsOfTClG9Cugb1ddBPfldU');
*/
// In metric unit. This is default
$distance_data = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?&origins='.urlencode($origin).'&destinations='.urlencode($destination).'&key=AIzaSyBg8mazOCFsHlsOfTClG9Cugb1ddBPfldU');
$distance_arr = json_decode($distance_data);
if ($distance_arr->status=='OK') {
 $destination_addresses = $distance_arr->destination_addresses[0];
 $origin_addresses = $distance_arr->origin_addresses[0];
 } else {
 echo "<p>The request was Invalid</p>";
 exit();
}
if ($origin_addresses=="" or $destination_addresses=="") {
  echo "<p>Destination or origin address not found</p>";
  exit();
}
// Get the elements as array
$elements = $distance_arr->rows[0]->elements;
$distance = $elements[0]->distance->text;
$duration = $elements[0]->duration->text;
setcookie("distance",$distance,time()+100);
echo "From: ".$origin_addresses."<br/> To: ".$destination_addresses."<br/> Distance: <strong>".$distance ."</strong><br/>";
echo "Duration: <strong>".$duration."";
}

?>
ADyson
  • 57,178
  • 14
  • 51
  • 63

0 Answers0