Results 1 to 16 of 16
-
04-08-2011, 03:36 PM #1Web Hosting Master
- Join Date
- Aug 2008
- Posts
- 1,679
Ip location switch statement help
Hi Everyone,
I have 3 javascript widgets, one for UK, one for Canada and the other for USA.
When a customer from France for example goes to any of them, he receives the error: widget unavailable in your country", that's all good. But the problem is that I cannot add the 3 widgets at the same time on the same page, there are javascript conflicts, therefore, the customer support of these widgets told me(and cannot help anymore) that I should write a switch statement with IPgeolocation so when a customer comes from USA, the script gives the USA widget, when he comes from Canada, it gives the canada widget, when the customer comes from UK, the script give the UK widget but when a customer comes from none of these locations, then a message saying "due to territorial license restrictions, you cannot view this content".
Any idea where I can download such script? I have never seen this work before and would not know where to start.
Thank you,
Ben
-
04-08-2011, 03:55 PM #2Web Hosting Master
- Join Date
- Aug 2008
- Posts
- 1,679
Ok I a half working switch statement and would need a little help to get it to work please, I have a fully loaded mysql with IPs , now the last part of the puzzle is the swItch statement:
Could this work?
switch($myCountryCode) {
case "UK":
echo "WIDGET JAVASCRIPTCODE HERE";
exit(0);
break;
case "US":
echo "WIDGET JAVASCRIPTCODE HERE";
exit(0);
break;
case "US":
echo "WIDGET JAVASCRIPTCODE HERE";
exit(0);
break;
default: echo "due to territorial license restrictions, you cannot view this content";
break;
}
Thank you,
Ben
-
04-09-2011, 01:20 AM #3Newbie
- Join Date
- Apr 2011
- Location
- United States
- Posts
- 23
notay latex
You can try using a GeoIP location script, there are a few free solutions out there actually.
This small code is just an example of how it can be used with WhatIsMyIPAddress.com's geodatabase.
I've commented the code to make you understand a bit more..
Code:$ip = $_SERVER['REMOTE_ADDR']; // self explanatory - gets ip $url=file_get_contents("http://whatismyipaddress.com/ip/$ip"); // goes to their geodb and grabs content // this just gets rid of the junk we dont need from the array preg_match_all('/<th>(.*?)<\/th><td>(.*?)<\/td>/s',$url,$output,PREG_SET_ORDER); //grabs the array Country: $country=$output[7][2]; // removes the img tag (messes up checks) // ill explain it below $country = preg_replace("/<img[^>]+\>/i", "", $country);
Now here it gets a bit more tricky with the PHP to the visible eye, because in the last line of code above I remove any img tags (because by default their database shows you an image of the country's flag) so it doesn't mess up PHP checks.
Code:<?php // NOTICE // there are two spaces after each country because of the img tag removal above // this is required so it will detect what country sucessfully. if($country == "United States ") { echo "widget code"; } elseif($country == "Canada ") { echo "widget code"; } elseif($country == "United Kingdom ") { echo "widget code"; } else { echo "Unfortunately, there is not a widget supported for your country yet.";
-
04-09-2011, 07:52 AM #4Web Hosting Master
- Join Date
- Aug 2008
- Posts
- 1,679
Thanks cmaniac!
Exactly what I wa slooking for, I have no time to try it today but tomorrow I will give it a try and see how it works|!!
Thank you so much,
Ben
-
04-09-2011, 08:33 AM #5WHT Addict
- Join Date
- Jun 2007
- Location
- Connecticut
- Posts
- 106
Nice post cmaniac!
Could you also utilize something like this to default the site language / google page translation, or would a simple redirect rule work better in this situation?Adam Piatek - CTO Ritmo Technology Group
Dedicated Servers, Web Hosting Solutions Ventrilo Servers
adam (at) ritmohost (dot) com
-
04-09-2011, 08:54 AM #6Web Hosting Master
- Join Date
- Aug 2008
- Posts
- 1,679
Actually I was dying to try your code :-)
I just gave it a go now but I m getting a parse error:
Parse error: syntax error, unexpected $end
is i not because of $country=$output[7][2];?
Thank you,
Ben
-
04-09-2011, 10:44 AM #7WHT Addict
- Join Date
- Apr 2005
- Posts
- 141
It is most likely because of the ?> missing. A switch is also cleaner than an if/elseif structure in this case. Which you were doing in the correct way though I don't think the exit was really needed because the break already ended the switch.
-
04-09-2011, 10:48 AM #8Web Hosting Master
- Join Date
- Aug 2008
- Posts
- 1,679
Hi Kemuel,
This is eactly how I have it on my page and still get the error:
<?php
$ip = $_SERVER['REMOTE_ADDR']; // self explanatory - gets ip
$url=file_get_contents("http://whatismyipaddress.com/ip/$ip"); // goes to their geodb and grabs content
// this just gets rid of the junk we dont need from the array
preg_match_all('/<th>(.*?)<\/th><td>(.*?)<\/td>/s',$url,$output,PREG_SET_ORDER);
//grabs the array Country:
$country=$output[7][2];
// removes the img tag (messes up checks)
// ill explain it below
$country = preg_replace("/<img[^>]+\>/i", "", $country);
// NOTICE
// there are two spaces after each country because of the img tag removal above
// this is required so it will detect what country sucessfully.
if($country == "United States ") {
echo "united state echo";
} elseif($country == "Canada ") {
echo "canada echo";
} elseif($country == "United Kingdom ") {
echo "uk echo code";
} else {
echo "Unfortunately, there is not a widget supported for your country yet.";
?>
Thank you,
Ben
-
04-09-2011, 10:50 AM #9WHT Addict
- Join Date
- Apr 2005
- Posts
- 141
You need another } before ?> to close the last elseif
-
04-09-2011, 11:08 AM #10Newbie
- Join Date
- Apr 2011
- Location
- United States
- Posts
- 23
Refer to,
At the end of my code I forgot an ending bracket, saw it and could not edit my post originally, sorry.
Code:<?php // NOTICE // there are two spaces after each country because of the img tag removal above // this is required so it will detect what country sucessfully. if($country == "United States ") { echo "widget code"; } elseif($country == "Canada ") { echo "widget code"; } elseif($country == "United Kingdom ") { echo "widget code"; } else { echo "Unfortunately, there is not a widget supported for your country yet."; } ?>
-
04-09-2011, 11:26 AM #11Newbie
- Join Date
- Apr 2011
- Location
- United States
- Posts
- 23
It is a bit cleaner but they'll both work in the same way, bambinou can always rewrite it in the future with the same ground code.
You could -- using Google Translate you can always detect their country origin and redirect to the URL where it translates your website, or if you translate the website yourself can redirect to "/lang/fr" "/lang/it" etc.
And one last time, here's the entire code:
Code:<?php $ip = $_SERVER['REMOTE_ADDR']; // self explanatory - gets ip $url=file_get_contents("http://whatismyipaddress.com/ip/$ip"); // goes to their geodb and grabs content // this just gets rid of the junk we dont need from the array preg_match_all('/<th>(.*?)<\/th><td>(.*?)<\/td>/s',$url,$output,PREG_SET_ORDER); //grabs the array Country: $country=$output[7][2]; // removes the img tag (messes up checks) // ill explain it below $country = preg_replace("/<img[^>]+\>/i", "", $country); // NOTICE // there are two spaces after each country because of the img tag removal above // this is required so it will detect what country sucessfully. if($country == "United States ") { echo "widget code"; } elseif($country == "Canada ") { echo "widget code"; } elseif($country == "United Kingdom ") { echo "widget code"; } else { echo "Unfortunately, there is not a widget supported for your country yet."; } ?>
Last edited by cmaniac; 04-09-2011 at 11:31 AM. Reason: Added full code.
-
04-09-2011, 02:00 PM #12Web Hosting Master
- Join Date
- Aug 2008
- Posts
- 1,679
Wow,wow,wow<---that's what I have to say!
IT WORKS!
Thanks you so much for that Cmaniac,
I am adding more code at the moment to get a new project on, I wil send you my working url(privately) when it is done :-)
Just one last question please, because we are feeding from another website to get the ip gea location, do you think we will be limited to only a certain amount of checks per month>?Any idea?
Thanks again!
-
04-09-2011, 02:46 PM #13Web Hosting Master
- Join Date
- Aug 2008
- Posts
- 1,679
That's very strange, it was working fine on my side and sudently no more ip recognitions, I keep getting the "Unfortunately, there is not a widget supported for your country yet." even with USA,uk or canada proxies.
Same for you?
Thank you,
Ben
-
04-09-2011, 02:57 PM #14Web Hosting Guru
- Join Date
- Apr 2008
- Posts
- 334
Ben,
Just check out what $country contains. Most IP lookup websites have daily limits, so this might be the problem.
PHP Code:echo "Unfortunately, there is not a widget supported for your country yet.<br/>";
echo $country;
-
04-09-2011, 03:01 PM #15Web Hosting Master
- Join Date
- Aug 2008
- Posts
- 1,679
Yes you are right, I have already reached the daily limit,dang!
I am now looking at an alternative here:
http://www.maxmind.com/app/ip-location
It looks like there is a free option on the right side of the page, but I am still struggling to understand how this works, I don't think I will be able to use cmaniac's code for this.....
-
04-09-2011, 04:33 PM #16Web Hosting Master
- Join Date
- Aug 2008
- Posts
- 1,679
Ok I found a nearly working solution,
<?php
include("geoip.inc");
$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
if($country == "US") {
echo 'usa';
} elseif($country == "CA") {
echo 'canada';
} elseif($country == "UK") {
echo 'grande bretagne';
} else {
echo 'this is none of them';
}
?>
Jusy go to maxwind and download the GeoIP.dat free file, for the geoip.inc you will have to find it on the net, it is easy.
It is working for USA but I struggle to get it to work with UK, I cannot find a single proxy that works well and get undected for this.
Ben
Similar Threads
-
c++ switch statement
By luxx in forum Programming DiscussionReplies: 12Last Post: 09-23-2009, 10:19 AM -
how to include a file within a switch statement?
By grabmail in forum Programming DiscussionReplies: 5Last Post: 03-09-2006, 01:43 PM -
$100USD switch compare $3000USD switch, whats the main difference?
By WCHost in forum Hosting Security and TechnologyReplies: 20Last Post: 02-14-2006, 10:21 PM -
consuming massive amounts of bandwidth- should I switch to co-location?
By disgust in forum Colocation, Data Centers, IP Space and NetworksReplies: 12Last Post: 10-17-2005, 03:15 PM -
Debug: PHP Switch Statement
By Jeanco in forum Programming DiscussionReplies: 12Last Post: 04-24-2005, 02:18 PM