Results 1 to 16 of 16
  1. #1

    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

  2. #2
    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

  3. #3
    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);
    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.
    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.";
    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.

  4. #4
    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

  5. #5
    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

  6. #6
    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

  7. #7
    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.

  8. #8
    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

  9. #9
    You need another } before ?> to close the last elseif

  10. #10
    Join Date
    Apr 2011
    Location
    United States
    Posts
    23
    Quote Originally Posted by bambinou View Post
    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
    Refer to,

    Quote Originally Posted by kemuel View Post
    You need another } before ?> to close the last elseif


    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.";
    }
    ?>
    Fixed code above.

  11. #11
    Join Date
    Apr 2011
    Location
    United States
    Posts
    23
    Quote Originally Posted by kemuel View Post
    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.
    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.



    Quote Originally Posted by adamnp View Post
    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?
    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.

  12. #12
    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!

  13. #13
    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

  14. #14
    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

  15. #15
    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.....

  16. #16
    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

  1. c++ switch statement
    By luxx in forum Programming Discussion
    Replies: 12
    Last Post: 09-23-2009, 10:19 AM
  2. how to include a file within a switch statement?
    By grabmail in forum Programming Discussion
    Replies: 5
    Last Post: 03-09-2006, 01:43 PM
  3. $100USD switch compare $3000USD switch, whats the main difference?
    By WCHost in forum Hosting Security and Technology
    Replies: 20
    Last Post: 02-14-2006, 10:21 PM
  4. consuming massive amounts of bandwidth- should I switch to co-location?
    By disgust in forum Colocation, Data Centers, IP Space and Networks
    Replies: 12
    Last Post: 10-17-2005, 03:15 PM
  5. Debug: PHP Switch Statement
    By Jeanco in forum Programming Discussion
    Replies: 12
    Last Post: 04-24-2005, 02:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •