Redirect Visitors based on City w/WebService API & PHP?

Discuss applications of MaxMind's GeoLite products

Redirect Visitors based on City w/WebService API & PHP?

Postby 7forward » Wed Apr 18, 2007 11:37 pm

Hello,

I am trying to set up a PHP script that is activated when visitors reach a specific webpage and will then redirect the visitor to a specific URL based on their closest city, but only when I have specified a unique URL for their location in the script. In the event that I have not specified a unique URL for their city, I want them to either stay on that page, or redirect to the a default URL.

So far I have the following script here:

http://www.buyershelpline.com/php/geotarget.php

------------------------------------------------------------------------------------

<?php
$query = "http://maxmind.com:8010/b?l=V69Wmy5jELay&i=$_SERVER[REMOTE_ADDR]";
$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$timeout = 1;
$fp = fsockopen ($host, 8010, $errno, $errstr, $timeout)
or die('Can not open connection to server.');
if ($fp) {
fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
while (!feof($fp)) {
$buf .= fgets($fp, 128);
}
$lines = split("\n", $buf);
$data = $lines[count($lines)-1];
fclose($fp);
} else {
# enter error handing code here
}
echo $data;
$geo = explode("-",$data);
$country = $geo[0];
$state = $geo[1];
$city = $geo[2];
$lat = $geo[3];
$lon = $geo[4];

// example given in MaxMind setup email: http://maxmind.com:8010/b?l=V69Wmy5jELay&i=24.24.24.24

php ?>

------------------------------------------------------------------------------------

http://www.buyershelpline.com/php/geotarget.php

This seems to be working at the basic level of identifying the location, but I have no idea how to change the script to redirect them based on their location. I'm just not sure where to take it from here. Any suggestions?
7forward
 
Posts: 2
Joined: Wed Apr 18, 2007 10:43 pm
Location: Santa Rosa, CA

Problem solved using search array

Postby 7forward » Thu Apr 19, 2007 5:20 am

My buddy helped me get it all figured out with a search array addition to the script. If you need this functionality, feel free to message me.

7forward
7forward
 
Posts: 2
Joined: Wed Apr 18, 2007 10:43 pm
Location: Santa Rosa, CA

yes

Postby red1000 » Mon May 14, 2007 9:43 pm

I am trying to do this also, but wasn't able to contact 7forward for advice because private messaging is disabled... any hints?
red1000
 
Posts: 1
Joined: Mon May 14, 2007 7:55 pm

Just passing

Postby Jobowo » Thu Jun 19, 2008 4:03 am

Just stumbled by and am not sure I understand exactly what's needed but...

1. You're querying the maxmind site to get the details for an IP? Why not install the database and PHP code provided here? The sample tells you how to use it.

2. No matter, you have the city etc. Is it as easy as:

switch($city) {
case "known1": $location = "http:[redirect 1.html]";break;
case "known2": $location = "http:[ ditto 2.html]";break;
..etc etc
default: $location = "http:default.html";break;
}
header("Location:$location");
exit;

Apologies if I've missed the point here
Jobowo
 
Posts: 10
Joined: Wed Jun 18, 2008 4:03 am

Ah...

Postby Jobowo » Thu Jun 19, 2008 4:10 am

OK, if you're using array_search you presumably have a larger number of cities than you want to deal with using this code so that code is...

$city_to_location = array(
array("city1"=>"url1"),
array("city2"=>"url2"),
etc etc));

$city = [wherever you get the city from];
if(isset($city_to_location[$city])
$location = $city_to_location[$city];
else
$location = $default_url;
header("Location:$location");
exit;
Jobowo
 
Posts: 10
Joined: Wed Jun 18, 2008 4:03 am


Return to GeoLite Applications

Who is online

Users browsing this forum: Google [Bot] and 3 guests