Home > Programming, Servers & Scripts >

Geo Redirects How are they done? Which apps, plug ins or db's do I need? (60)


03-07-2013 08:09 PM #1 sandyone (Member)
Geo Redirects How are they done? Which apps, plug ins or db's do I need?

What do I need to self host geo redirect capabilities?

I know of the db's from MaxMind named GeoLite.

What do I do with that?

Do I use a plug in such as : geoplugin.com ?

Thank you.


03-07-2013 09:53 PM #2 sandyone (Member)

Ok Support has installed the GeoLite Country db.

Does my any php or java scirpt i add to my page have to include the db's log in details?


03-07-2013 10:49 PM #3 zeno (Administrator)

This may help ya: woops wrong link, nevermind.

Don't use Geoplugin.class. You need to 1) test that your geoip PHP extension is working and 2) get a handle on the PHP scripting required to do redirects and things. Ugh I should have made my video by now. I will get that done asap.


03-07-2013 11:52 PM #4 sandyone (Member)

I have searched and copied the various scripts I found here.

I'll take baby steps to learn how to employ the db onto a page.


03-08-2013 04:47 AM #5 aeroian (Member)

I use Maxmind's database for geoip. Essentially, you have to first install the db on your server somewhere (look for example files on the maxmind site as to where to install it). Assuming you use PHP to interact with this database; when you want to actually use it, you'll include a PHP file that references that database. Its a little tricky the first time you do it, but after that it gets really easy Let me know if you want a tutorial or something.

~Ian


03-08-2013 05:10 AM #6 sandyone (Member)

aeroian,

I sent you a PM of what my Host support said they did for installing the GeoLite Country DB.

I have no idea how to add the php or js to a page so I may redirect to a safe offer IF the visitor is from a wrong country for an offer.

Thank you.


03-08-2013 05:46 AM #7 sandyone (Member)

Is this it? This goes on my pages? How does it log into the geolite db? I don't see any user or password for the db.

Code:
 <?php

$keyword = $_GET['keyword'];

$geo = geoip_record_by_name( $_SERVER['REMOTE_ADDR'] );
$countrycode = $geo['country_code'];

switch($countrycode) {
    case 'US':
        $redirect[1] = 'http://domain.com/t/base.php?c=XX&key=xxxxxxxxxxxxxx&keyword=US-'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'CA':
        $redirect[1] = 'http://domain.com/t/base.php?c=XX&key=xxxxxxxxxxxxxx&keyword=US-'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'GB':
        $redirect[1] = 'http://domain.com/t/base.php?c=XX&key=xxxxxxxxxxxxxx&keyword=US-'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'AU':
        $redirect[1] = 'http://domain.com/t/base.php?c=XX&key=xxxxxxxxxxxxxx&keyword=US-'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'NZ':
        $redirect[1] = 'http://domain.com/t/base.php?c=XX&key=xxxxxxxxxxxxxx&keyword=US-'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'IE':
        $redirect[1] = 'http://domain.com/t/base.php?c=XX&key=xxxxxxxxxxxxxx&keyword=US-'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;

    default:
        $message = 'Traffic from: '.$countrycode.' : '.$keyword;
        $timestamp = date('d/m/Y H:i:s');
        $log_file = 'logs/Logs_script.log';     
        error_log('['.$timestamp.'] INFO: '.$message.PHP_EOL, 3, $log_file);
        $redirect[1] = 'http://domain.com/t/base.php?c=XX&key=xxxxxxxxxxxxxx&keyword='.$countrycode.'-'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
} 
?>


03-08-2013 10:17 AM #8 caurmen (Administrator)

Hmm - looks to me like that's the right code. If your host has installed php5-geoip, it should Just Work (TM).

Have you tried running it, and if so, what happened?

(Incidentally, I have "geolocation" on my Big List Of Useful Things To Have A Tutorial About, and will write a tutorial about it some time - but of course if anyone else wants to write a tutorial about it first, please do!)


03-18-2013 05:44 AM #9 paycoguy (Member)

Can someone please make a goddam video of how to redirect users of different countries to each offer? I tried asking Liquid Web but they have no fucking clue on how to do this.


03-18-2013 06:03 AM #10 sandyone (Member)

You'd think that someone would have created an app for this by now.

Or incorporated it into their tracker.


03-18-2013 11:40 AM #11 caurmen (Administrator)

I'm going to be writing a tutorial for geo-redirects at some point in the next couple of months. Given how many people are having trouble with it, I may look into doing it sooner rather than later!


03-18-2013 02:43 PM #12 2good4u ()

This is the code i use in my geo landers;

<?php
if ( function_exists ('geoip_record_by_name') ) {
$geo = geoip_record_by_name( $_SERVER['REMOTE_ADDR'] );
extract($geo);
if ( $country_name == "Countryname" ) {
echo "<meta http-equiv=\"refresh\" content=\"0;url=http://www.website.com\">";
}
if ( $country_name == "Countryname2" ) {
echo "<meta http-equiv=\"refresh\" content=\"0;url=http://www.website2.com\">";
}
else {
echo "<meta http-equiv=\"refresh\" content=\"0;url=http://www.website3.com\">";
}

}
?>
PS: Geolite is required.


03-18-2013 02:46 PM #13 julien (Member)

Quote Originally Posted by caurmen View Post
I'm going to be writing a tutorial for geo-redirects at some point in the next couple of months. Given how many people are having trouble with it, I may look into doing it sooner rather than later!
I'm very interested in this tutorial Caurmen.
Thank you very much for your work


03-18-2013 03:56 PM #14 paycoguy (Member)

Quote Originally Posted by 2good4u View Post
This is the code i use in my geo landers;


PS: Geolite is required.
How does this reference your server? Doesn't it need a domain listed in the code?


03-18-2013 06:55 PM #15 sandyone (Member)

caurmen,

The sooner you devote your time to the things that interest me, the sooner we can all move on with our lives


Does using such a script cause an issue if the FB reviewer is redirected?


03-20-2013 02:02 AM #16 sandyone (Member)

I saved the following as trythis.php and put it on one of my add on domains through its own cpanel , and it does nothing.

I was hoping it would redirect those listed nations , US,CA,GB,AUD etc to Vonage.com.

I have geolite installed on my VPS server.

I have no idea what to try next. Any help will be appreciated.

Code:
<?php

$keyword = $_GET['keyword'];

$geo = geoip_record_by_name( $_SERVER['REMOTE_ADDR'] );
$countrycode = $geo['country_code'];

switch($countrycode) {
    case 'US':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'CA':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'GB':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'AU':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'NZ':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'IE':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;

    default:
        $message = 'Traffic from: '.$countrycode.' : '.$keyword;
        $timestamp = date('d/m/Y H:i:s');
        $log_file = 'logs/Logs_script.log';     
        error_log('['.$timestamp.'] INFO: '.$message.PHP_EOL, 3, $log_file);
        $redirect[1] = 'http://vonage.com'.$countrycode.'-'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
} 
?>


03-20-2013 01:06 PM #17 caurmen (Administrator)

It looks to me like you're missing a ? symbol - so the URL that your script is trying to redirect to will end up being, say, http://vonage.comwomble if your keyword is "womble".

Try changing your redirects to

Code:
$redirect[1] = 'http://vonage.com/?keyword='.$keyword;
That'll give an eventual URL of http://vonage.com/?keyword=womble, which should redirect properly.


03-20-2013 01:47 PM #18 bbrock32 (Administrator)

Just e heads up , the new STM mobile tracker does have this built in.

It will be released this week


03-20-2013 02:02 PM #19 doppelganger (Member)

I've been using MaxMind's web services for geoip. I can buy 200,000 country lookups for $20. http://www.maxmind.com/en/web_services

Does anyone know if there is a noticeable difference in speed when using the web service vs a self hosted database? It seems logical that a self hosted solution would be faster but I wonder if it's actually enough to be concerned about.

Here's my PHP code for invoking the service if anyone's interested

Code:
function getMaxMindData($ip)
{
	$query = MAXMIND_URL . '?l=' . MAXMIND_KEY . '&i=' . $ip;
	$url = parse_url($query);
	$host = $url["host"];
	$path = $url["path"] . "?" . $url["query"];
	$timeout = 1;
	$fp = fsockopen ($host, 80, $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 = explode("\n", $buf);
	  $data = $lines[count($lines)-1];
	  fclose($fp);
	} 
	else {
	  # enter error handing code here
	}
	writeToLog($data."\n\n\n");
	return $data;
}


03-20-2013 03:45 PM #20 caurmen (Administrator)

I'd be a little concerned about speed impacts with a web service, but fortunately there's a really easy way to check the impact on your page (which you may well already have thought of!)

Just create a test page using this code, set a variable with gettimeofday() before you do the lookup, then use gettimeofday() again after the lookup has returned a result, and echo the difference to screen.

If you do this, I'd be very interested to see the result!


03-20-2013 06:06 PM #21 doppelganger (Member)

Quote Originally Posted by caurmen View Post
I'd be a little concerned about speed impacts with a web service, but fortunately there's a really easy way to check the impact on your page (which you may well already have thought of!)

Just create a test page using this code, set a variable with gettimeofday() before you do the lookup, then use gettimeofday() again after the lookup has returned a result, and echo the difference to screen.

If you do this, I'd be very interested to see the result!
Average response time was 0.053 seconds ranging from 0.051-0.054.

-Aaron


03-20-2013 06:27 PM #22 caurmen (Administrator)

Whoa. OK, that's pretty darn swift - can't imagine you'll see much of a CTR drop from a 1/20th of a second wait!

Nice one! Thanks for testing!


03-20-2013 08:26 PM #23 sandyone (Member)

Quote Originally Posted by bbrock32 View Post
Just e heads up , the new STM mobile tracker does have this built in.

It will be released this week

Great!

Then one could redirect as desired for mobile or non mobile!


03-20-2013 08:28 PM #24 sandyone (Member)

Quote Originally Posted by caurmen View Post
It looks to me like you're missing a ? symbol - so the URL that your script is trying to redirect to will end up being, say, http://vonage.comwomble if your keyword is "womble".

Try changing your redirects to

Code:
$redirect[1] = 'http://vonage.com/?keyword='.$keyword;
That'll give an eventual URL of http://vonage.com/?keyword=womble, which should redirect properly.

Is the only way to redirect in conjunction with a keyword?

Can I just redirect using only a url sans any keywords?

Gracias!

Coud I remove the line with:
<?php
$keyword = $_GET['keyword'];

Also, I saved this with nothing but the php on a page, do I need to save this with any header info such as:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>


03-20-2013 08:35 PM #25 caurmen (Administrator)

You don't need a keyword at all - in fact, you could just simplify to

Code:
    case 'US':
        header("Location:http://vonage.com");
        exit;


03-20-2013 09:11 PM #26 sandyone (Member)

In the script does it require that I add where the geolite DB is?

I have now tested with the added question marks , without, in root of my addon domain, in the public_hrml of my doamin, with and without HTML header.

It does nothing at all.

Code:
$geo = geoip_record_by_name( $_SERVER['REMOTE_ADDR'] );


03-20-2013 09:13 PM #27 sandyone (Member)

Hosting support has done the following for me several days ago:

"We have installed apache module "mod_geoip" in the server. Please verify the details given below.

---
root@host [~]# httpd -M | grep geoip
Syntax OK
geoip_module (shared)
---

We have enabled geoip in the server. Please verify the details given below.

---
root@host [~]# php -i | grep geoip
geoip
geoip support => enabled
geoip extension version => 1.0.8
geoip library version => 1004008
---

Also we have added geoIP country database in the server and added geoIP rules in the ".htaccess" file. They are working fine. Please check this from your end."


03-20-2013 11:39 PM #28 zeno (Administrator)

Quote Originally Posted by doppelganger View Post
Average response time was 0.053 seconds ranging from 0.051-0.054.

-Aaron
Pretty fast, not hugely surprising if it's a US based server communicating with another US based server. But I think 55 ms is still going to be slower than doing the lookup yourself, since it's basically doing the same thing but you're adding in all the time wasted connecting to and receiving replies from another server.

Quote Originally Posted by sandyone View Post
Hosting support has done the following for me several days ago:
Also we have added geoIP country database in the server and added geoIP rules in the ".htaccess" file. They are working fine. Please check this from your end."[/I]
Why has anything been added to your .htaccess? That seems peripheral to anything you want to do.


03-20-2013 11:59 PM #29 sandyone (Member)

Would 50 bucks be enough on Freelancer to get this done?


03-21-2013 02:13 AM #30 2good4u ()

Quote Originally Posted by sandyone View Post
I saved the following as trythis.php and put it on one of my add on domains through its own cpanel , and it does nothing.

I was hoping it would redirect those listed nations , US,CA,GB,AUD etc to Vonage.com.

I have geolite installed on my VPS server.

I have no idea what to try next. Any help will be appreciated.

Code:
<?php

$keyword = $_GET['keyword'];

$geo = geoip_record_by_name( $_SERVER['REMOTE_ADDR'] );
$countrycode = $geo['country_code'];

switch($countrycode) {
    case 'US':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'CA':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'GB':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'AU':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'NZ':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'IE':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;

    default:
        $message = 'Traffic from: '.$countrycode.' : '.$keyword;
        $timestamp = date('d/m/Y H:i:s');
        $log_file = 'logs/Logs_script.log';     
        error_log('['.$timestamp.'] INFO: '.$message.PHP_EOL, 3, $log_file);
        $redirect[1] = 'http://vonage.com'.$countrycode.'-'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
} 
?>
Are you sure you have geolite installed in your server?
I tried that code in my server which has geolite installed and it did work.
Please, add this code to a php file to make sure geolite is installed, i m thinking they didn't install it properly;
<?php
if ( function_exists ('geoip_record_by_name') ) {
$geo = geoip_record_by_name( $_SERVER['REMOTE_ADDR'] );
extract($geo);
echo "<h1>How's the weather in $city, $region , $country_code</h1>";
}
phpinfo();
?>?>


03-21-2013 03:33 AM #31 sandyone (Member)

I tried using the text in the root of the domain and the public_html as well- nothing happens at all.

I'll ask support to try the text php


03-21-2013 03:48 AM #32 zeno (Administrator)

You're speaking in riddles Sandy, I'm having a hard time understanding what it is you're actually doing o.O


03-21-2013 03:58 AM #33 thomasbhm (Member)

Hey Sandyone,

I'm pretty sure if you want to use the Maxmind GEO IP Lite you need to include .dat at the top of your code.

Should be something like this in your code:

$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

Use the absolute path to your .dat file. If it's in same folder as your php this example should work.

The .dat file is part of the download and it has all of the database info your code uses to reference things.


03-21-2013 04:12 AM #34 sandyone (Member)

I am trying to add the ability to geo redirect based in code used by the MaxMind GeoLite database.
I am trying to ad this to my VPS in such as way that any add on domain can do geo redirects.

Example using the code beleow:

Send visitors from an IP that GeoLite MaxMind Database says are from the : US , CA, GB ,AU, NZ and IE would be sent to the url : www.vonage.com



Code:
<?php

$keyword = $_GET['keyword'];

$geo = geoip_record_by_name( $_SERVER['REMOTE_ADDR'] );
$countrycode = $geo['country_code'];

switch($countrycode) {
    case 'US':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'CA':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'GB':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'AU':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'NZ':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
    case 'IE':
        $redirect[1] = 'http://vonage.com'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;

    default:
        $message = 'Traffic from: '.$countrycode.' : '.$keyword;
        $timestamp = date('d/m/Y H:i:s');
        $log_file = 'logs/Logs_script.log';     
        error_log('['.$timestamp.'] INFO: '.$message.PHP_EOL, 3, $log_file);
        $redirect[1] = 'http://vonage.com'.$countrycode.'-'.$keyword;
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
} 
?>


03-21-2013 04:17 AM #35 sandyone (Member)

Quote Originally Posted by thomasbhm View Post

Use the absolute path to your .dat file. If it's in same folder as your php this example should work.

So I need to ad that code and save the php in the same file where support put my GeoMAxMind?

I don't save the php in the root of my addon domains?

Thank you.


03-21-2013 04:26 AM #36 zeno (Administrator)

To clarify some things for ya:

1. Georedirection databases and corresponding PHP extension(s) are installed on your server - your server works with these, addon domains are meaningless - a "domain" doesn't process anything. An analogy would be wanting to install Microsoft Word (GeoIP stuff) on your computer (server), and then getting panicked about wanting Word documents (GeoIP stuff in PHP scripts) to be able to be opened from any folder (domain) on your computer.

2. PHP is a programming language. Think of it like javascript or HTML. You don't put 'it' in the 'root' of your server or any domains public_html folder. PHP files are basically web pages. They get loaded in your browser and then do something specific. Instead of displaying content, your PHP file redirects the user somewhere. So you would upload it wherever you would a normal webpage, and load it exactly like a normal webpage, and your advert would point to it as if it is a normal webpage. Because it is.


03-21-2013 07:43 AM #37 thomasbhm (Member)

Quote Originally Posted by sandyone View Post
So I need to ad that code and save the php in the same file where support put my GeoMAxMind?

I don't save the php in the root of my addon domains?

Thank you.
Well you will have to modify it a little that was just an example. Looks like you are using the $countrycode variable for country so you want something like this:

$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$countrycode = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

replace these two lines w/ the code above:

$geo = geoip_record_by_name( $_SERVER['REMOTE_ADDR'] );
$countrycode = $geo['country_code'];

Extract the files from the .gzip for the GeoLite Country database from GeoMaxMind and you will get the the .dat file I'm referring to. Drop the .dat in the root (of your addon domain) along w/ your php file that is doing the redirecting.

http://dev.maxmind.com/geoip/geolite


03-21-2013 07:50 AM #38 sandyone (Member)

Thank you.

I'll give it a go.


03-21-2013 07:55 AM #39 zeno (Administrator)

Quote Originally Posted by thomasbhm View Post
Well you will have to modify it a little that was just an example. Looks like you are using the $countrycode variable for country so you want something like this:

$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$countrycode = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

replace these two lines w/ the code above:

$geo = geoip_record_by_name( $_SERVER['REMOTE_ADDR'] );
$countrycode = $geo['country_code'];

Extract the files from the .gzip for the GeoLite Country database from GeoMaxMind and you will get the the .dat file I'm referring to. Drop the .dat in the root (of your addon domain) along w/ your php file that is doing the redirecting.

http://dev.maxmind.com/geoip/geolite
This is a much more complicated approach than necessary - the installed PHP extension and C API handle the interaction with the database stored at one specific location, rather than copying it all over the place and trying to read from it directly in your scripts.


03-21-2013 08:30 AM #40 sandyone (Member)

If guys that know this can't agree, I have no hope of being able to do this.

Ill try and outsource this.


03-21-2013 08:49 AM #41 sandyone (Member)

Quote Originally Posted by 2good4u View Post
Are you sure you have geolite installed in your server?
I tried that code in my server which has geolite installed and it did work.
Please, add this code to a php file to make sure geolite is installed, i m thinking they didn't install it properly;
I did learn they did have something installed. I read at another forum that that's needed, Net_GeoIP.


Support now says:

"The pear module Net_GeoIP was not installed in the server. I have installed it now.

--
# pear list | grep geo -i
Net_GeoIP 1.0.0 stable
--

I have checked the weather scripts you have used. I could see that you are using the following code.

--
echo "
How's the weather in $city, $region , $country_code
";
--

Here, you have not assigned any values to the variables $city, $region and $country_code. I am not sure how it will work without any value assignment.

Mod_geoIP is installed and configured in apache and PHP in the server.

Please contact your developer and check the scripts."


03-21-2013 09:33 AM #42 zeno (Administrator)

You should get the GeoIP C modules and PHP extension installed by your host rather than an outsourcer... reason being you need root access to do this properly and that's not really something you want to give to a random. Hiring someone for testing and showing you how to get things working is fine though.


03-21-2013 10:00 AM #43 thomasbhm (Member)

Quote Originally Posted by sandyone View Post
If guys that know this can't agree, I have no hope of being able to do this.

Ill try and outsource this.
Here you go man. I set this up using the code you posted.

Just unzip it and drop the files from the STM-GEO folder into the root of your add-on domain:

http://www.mediafire.com/download.php?nl7x5hs6785dbaj

index.php is the file w/ your code in it that does the re-direct.

Also, I changed all your instances of vonage to the appropriate google site per the country the person is in.

AND I removed the references to $keyword. I assume you copied that code from somewhere and it adds functionality to append a keyword to the end of the URL if there was one in the url string that called the page.

No reason to overcomplicate things.


03-21-2013 10:07 AM #44 Oded Abbou (Member)

Quote Originally Posted by thomasbhm View Post
Here you go man. I set this up using the code you posted.

Just unzip it and drop the files from the STM-GEO folder into the root of your add-on domain:

http://www.mediafire.com/download.php?nycpa51capcv0ma

index.php is the file w/ your code in it that does the re-direct.

Also, I changed all your instances of vonage to the appropriate google site per the country the person is in.

AND I removed the references to $keyword. I assume you copied that code from somewhere and it adds functionality to append a keyword to the end of the URL if there was one in the url string that called the page.

No reason to overcomplicate things.

Perfect! thanks a lot!


03-21-2013 11:45 AM #45 julien (Member)

Quote Originally Posted by thomasbhm View Post
Here you go man. I set this up using the code you posted.

Just unzip it and drop the files from the STM-GEO folder into the root of your add-on domain:

http://www.mediafire.com/download.php?nycpa51capcv0ma

index.php is the file w/ your code in it that does the re-direct.

Also, I changed all your instances of vonage to the appropriate google site per the country the person is in.

AND I removed the references to $keyword. I assume you copied that code from somewhere and it adds functionality to append a keyword to the end of the URL if there was one in the url string that called the page.

No reason to overcomplicate things.
Works like a charm.
Thank you so much!


03-21-2013 03:37 PM #46 caurmen (Administrator)

@thomashbm - Awesome stuff!


03-21-2013 07:35 PM #47 sandyone (Member)

Thank you Sir Thomas!

I shall get to working on it!

Thank you!


03-21-2013 08:05 PM #48 sandyone (Member)

There is something fundamentally wrong on my VPS.

It does nothing.

Think I'll close it down and start new service.


03-22-2013 12:14 AM #49 sandyone (Member)

YEAH!

Thomas's files work for me.

When I place them in my domains public_html , I can redirect based on Geo-Ips !

We can all relax and go about our lives once again!


Thomashbm - pick any girl in my village and she is yours.


03-22-2013 12:40 AM #50 sandyone (Member)

So the requirements to Geo Redirect using MaxMind GeoLite are:

Installation of:

The pear module Net_GeoIP was not installed in the server
The MaxMind GeoLite database

in ones public_html directory:
geoip.dat file
geoip.inc file
Your php code


03-22-2013 01:23 AM #51 sandyone (Member)

So I may learn something, what does the following do:


03-22-2013 01:54 AM #52 zeno (Administrator)

Quote Originally Posted by sandyone View Post
So the requirements to Geo Redirect using MaxMind GeoLite are:

Installation of:

The pear module Net_GeoIP was not installed in the server
The MaxMind GeoLite database

in ones public_html directory:
geoip.dat file
geoip.inc file
Your php code
Yes. Need databases from MaxMind database + PHP API for them, or C API + PECL PHP extension. I prefer the PECL branch as it is known to be a much faster system. As for the latter part, I wouldn't do it that way since it requires the geoip files being in the same dir as every script (unless you changed the coding to include from root dir). Imagine updating your database...

It is a great place to start though for people who have little idea about what's going on.

As for all the other stuff:
The switch statement in PHP needs a default section for when the variable ($countrycode) does not match any cases you give it.
$message is a variable being set to "Traffic from: US" for example
$timestamp = a timestamp
Following this one might dump this info to a text file to log clicks that have come from outside of the geos you want and when they occurred. As it is right now, you can remove those lines as they are redundant.
$number = mt_rand(1,1) produces a number between 1 and 1. This is useful for rotating between links. Say you put $number = mt_rand(1,3), it will spit out 1, 2 or 3. Then you have 3 lines of links, $redirect[1], $redirect[2], $redirect[3]. At the header() bit you specify location as $redirect[$number] which causes it to randomly rotate through links 1, 2 and 3.


03-22-2013 03:50 AM #53 thomasbhm (Member)

Quote Originally Posted by sandyone View Post
So the requirements to Geo Redirect using MaxMind GeoLite are:

Installation of:

The pear module Net_GeoIP was not installed in the server
The MaxMind GeoLite database

in ones public_html directory:
geoip.dat file
geoip.inc file
Your php code
No, you just need those three files in your public_html directory for this script to work. That's it.

There is no need to install a database, it is reading the info from the .dat file, not a database.

There is also no need to install any other extensions, it's self inclusive, everything you need is in the zip.

If you want to use one geoip.dat file for multiple domains (so you dont have to update it in multiple locations) just install in a directory above your add-on domain and link to it using an absolute path. Your webhost should be able to help you with that if you get to that point.

Quote Originally Posted by zeno View Post
Following this one might dump this info to a text file to log clicks that have come from outside of the geos you want and when they occurred. As it is right now, you can remove those lines as they are redundant.
Good point, I removed the lines writing to the log file, but I left those two in. Shouldn't hurt to leave them there, but they aren't necessary. Also I should mention that the way it currently works is if the visitor is in a country where the country code isn't in your switch it redirects to a domain you set w/ their country code as a subfolder.

ex: google.com/us or google.com/uk etc.

If you want to change that just modify the default to this:

Code:
    default:
        $redirect[1] = 'http://google.com/';
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
That will send all sites that aren't specified in the switch to one default site. Now that I see others are using this I'll update it since most wont have directories set up for countries outside the switch. It was also an artifact from the code you posted.

Here is the updated code w/ the new default: http://www.mediafire.com/download.php?nl7x5hs6785dbaj


03-22-2013 04:03 AM #54 thomasbhm (Member)

Quote Originally Posted by zeno View Post
$number = mt_rand(1,1) produces a number between 1 and 1. This is useful for rotating between links. Say you put $number = mt_rand(1,3), it will spit out 1, 2 or 3. Then you have 3 lines of links, $redirect[1], $redirect[2], $redirect[3]. At the header() bit you specify location as $redirect[$number] which causes it to randomly rotate through links 1, 2 and 3.
I was wondering what that was for Very useful!


03-22-2013 04:09 AM #55 d180304 (Member)

i like maxmind..


03-22-2013 04:32 AM #56 sandyone (Member)

That's what its using MAxMind GeoLite Db the country one.

No cities.


03-22-2013 05:46 AM #57 johnnybegood (Member)

Came across this in case its useful:


1) Place the following in the <head>
<script language=”JavaScript” src=”http://j.maxmind.com/app/geoip.js”></script>

2) Use the following in the <body> where you want the information placed. Use one or all of them its up to you.

Country Code: ie US
<script language=”JavaScript”>document.write(geoip_country _code());</script>

Country Name: ie United States
<script language=”JavaScript”>document.write(geoip_country _name());</script>

City:
<script language=”JavaScript”>document.write(geoip_city()) ;</script>

Region: ie NY
<script language=”JavaScript”>document.write(geoip_region( ));</script>

Region Name: ie New York
<script language=”JavaScript”>document.write(geoip_region_ name());</script>

Latitude:
<script language=”JavaScript”>document.write(geoip_latitud e());</script>

Longitude:
<script language=”JavaScript”>document.write(geoip_longitu de());</script>

Postal Code:
<script language=”JavaScript”>document.write(geoip_postal_ code());</script>


03-22-2013 06:08 AM #58 sandyone (Member)

Hi Johhny,

What will this do?

And it's using js?


04-03-2013 12:59 AM #59 sandyone (Member)

Thomas,

If I only want to redirect traffic coming from one nation, is it ok to delete the other lines such as:

Code:
    case 'GB':
        $redirect[1] = 'http://www.google.com/';
        $number = mt_rand(1,1);
        header("Location:$redirect[$number]");
        exit;
I could just redirect one nation by just having one stance of the Case 'nation'- right?


04-03-2013 10:52 AM #60 caurmen (Administrator)

@sandy - unless the script is doing something very unusual, yes, that should be correct. If thomasbhm corrects me on this, though, listen to him as it's his script!


Home > Programming, Servers & Scripts >