Jump to content

PHP to Geo target content for country.


the-sexy-slug

Recommended Posts

Hello

 

I am trying to show/geo target content for certian countries that my visitors to my site are from.

 

I have doing a little research on google and have found a few script but for some reason they do not work.

 

I know about the geoip database from maxmind but I am looking for a remote solution as the geoip database is said to be a heavy strain on servers if you get a lot of visitors. I am hosted on a shared server so I do not want to get my account suspended for over working the server. Also I have a few sites that will use this idea.

 

This is a script I found whilst googling and all it does is give me an error.

 

<?php 
$ip = $_SERVER [ 'REMOTE_ADDR' ]; 
$country = file_get_contents ( 'http://api.hostip.info/country.php?ip=' . $ip ); 
if ( $country == "US" ) { 
echo "<American-English Ringtone Ad Tag>" ; } 
elseif ( $country == "AU" OR $country == "UK" OR $country == "IE" ) { 
echo "<British-English Ringtone Ad Tag>" ; } 
else { 
echo "<YPN Ad Tag>" ; } 
?>

 

 

This is the error it gives me:-

 

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/username/public_html/webaddress.co.uk/test/test.php on line 7

 

What I am trying to do is to geo target Javascript ads for different countries. When I add the javascript ad it gives me that error. If I just put in normal text then it works ok.

 

Thank you all for your help in advance.

 

Link to comment
Share on other sites

<?php 
$ip = $_SERVER['REMOTE_ADDR'];
$country = file_get_contents('http://api.hostip.info/country.php?ip='.$ip);
if ($country=="US") {
  echo "<American-English Ringtone Ad Tag>" ;
} elseif ($country=="AU" || $country=="UK" || $country=="IE") {
  echo "<British-English Ringtone Ad Tag>";
} else {
  echo "<YPN Ad Tag>";
}
?>

Couple of things...

1. PHP uses || instead of OR

2. If displaying < or > on a web page you need to use the codes otherwise the browser will try and interpret the contents as HTML and nothing will be displayed

Link to comment
Share on other sites

As an extra, if you're choosing multiple country codes a switch() conditional would be better.

switch ($country) {
  case "US":
    echo "<American-English Ringtone Ad Tag>";
    break;
  case "UK":
  case "IE":
  case "AU":
    echo "<British-English Ringtone Ad Tag>";
    break;
  default:
    echo "<YPN Ad Tag>";
}

Link to comment
Share on other sites

Thank you for yout reply.

 

That seams to work with text but when I put in a javascript ad code it gives me an error:-

 

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/username/public_html/webaddress.co.uk/test/test.php on line 9

 

Any advice to help me show javascript ads for different locations would be very much appreciated. I have been trying to solve this problem now for months.

 

Thanks all in advance for your advice.

Link to comment
Share on other sites

You can post your Javascript and I'll have a quick look but you're better off posting that in the Javascript section - also because if I can't sort it someone else might.

 

As for the above script, if hostip.info ever goes down or has difficulties your web page will have until file_get_contents() times out. I just knocked this up using cURL that will wait a maximum of 2 seconds - you can change this to however long you want your script to wait, replace the file_get_contents line with the following:

$ch=curl_init('http://api.hostip.info/country.php?ip='.$ip);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER,false);
curl_setopt($ch,CURLOPT_TIMEOUT,2);
$country=curl_exec($ch);
if (curl_errno($ch)) {
  echo 'ERROR';
}
curl_close($ch);

Link to comment
Share on other sites

Thanks for the reply.

 

I have changed the > and < with the < etc as you suggested and I still get the same error.

 

What I am trying to do is to post a ebay affiliate link that I created using there ebay tool kit program. It produces a javascript code that you paste in your website. And for some reason it is not working when I paste into the php.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.