Portal Home > Knowledgebase > Articles Database > Ip location switch statement help


Ip location switch statement help




Posted by bambinou, 04-08-2011, 03:36 PM
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

Posted by bambinou, 04-08-2011, 03:55 PM
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

Posted by cmaniac, 04-09-2011, 01:20 AM
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.. Then you will have PHP check the country (the $country variable) and determine which widget to load and if their country is not found display an error message. 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. Then the widget code will only be executed depending on their country origin. Sorry for the messy code and weird checking skills this was my easiest solution.

Posted by bambinou, 04-09-2011, 07:52 AM
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

Posted by adamnp, 04-09-2011, 08:33 AM
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?

Posted by bambinou, 04-09-2011, 08:54 AM
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

Posted by kemuel, 04-09-2011, 10:44 AM
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.

Posted by bambinou, 04-09-2011, 10:48 AM
Hi Kemuel, This is eactly how I have it on my page and still get the error: (.*?)<\/th>(.*?)<\/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("/]+\>/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

Posted by kemuel, 04-09-2011, 10:50 AM
You need another } before ?> to close the last elseif

Posted by cmaniac, 04-09-2011, 11:08 AM
Refer to, At the end of my code I forgot an ending bracket, saw it and could not edit my post originally, sorry. Fixed code above.

Posted by cmaniac, 04-09-2011, 11:26 AM
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: Last edited by cmaniac; 04-09-2011 at 11:31 AM. Reason: Added full code.

Posted by bambinou, 04-09-2011, 02:00 PM
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 particular amount of checks per month>?Any idea? Thanks again!

Posted by bambinou, 04-09-2011, 02:46 PM
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

Posted by WootWoot, 04-09-2011, 02:57 PM
Ben, Just check out what $country contains. Most IP lookup websites have daily limits, so this might be the problem.

Posted by bambinou, 04-09-2011, 03:01 PM
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.....

Posted by bambinou, 04-09-2011, 04:33 PM
Ok I found a nearly working solution, 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



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
jodohost.com?? (Views: 634)