Page 1 of 1

php redirect

Posted: Wed May 04, 2011 5:01 pm
by kratos-2012
Please help! Is there a way to modify the php code so that it checks the longitude and latitude with the database and redircts it to the appropriate url based on the return results?

Thanks in advance

Here is the html code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Your Address:</title>
<script type="text/javascript">
navigator.geolocation.getCurrentPosition(function(position) {
lat = position.coords.latitude;
lang = position.coords.longitude;
window.location.href = "fetch.php?lat=" + lat + "&long=" + lang;
});
</script>
</head>
<body onload="getCurrentPosition()"></body>
</html>




and PHP code


<?php
$jsonurl = "http://maps.googleapis.com/maps/api/geo ... ensor=true";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
echo "<h2>Your address is </h2>";
echo $json_output->results[0]->formatted_address;
?>

Re: php redirect

Posted: Mon May 16, 2011 12:27 pm
by Jade
You'd have to update your fetch.php page to find a result and then to a header redirect to the appropriate page.

Code: Select all

//this is your fetch.php

$long = mysql_real_escape_string($_GET['long']);
$lat = mysql_real_escape_string($_GET['lat']);

//run the query
$query = mysql_query("SELECT url FROM location WHERE long='$long' AND lat='$lat'") or die (mysql_error());
$location = mysql_fetch_assoc($query);

if ($location) //found a result
{
   header("Location:" . $location['url']);
   exit;
}

//otherwise redirect them back to whatever page they were on