php redirect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kratos-2012
Forum Newbie
Posts: 4
Joined: Tue Aug 10, 2010 3:06 pm

php redirect

Post 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;
?>
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: php redirect

Post 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
Post Reply