Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder
    Join Date
    Aug 2013
    Posts
    149
    Thanks
    21
    Thanked 0 Times in 0 Posts

    Redirecting Page by Country

    RESOLVED: I was sure this code was correct so I messed around in my php config.I edited allow_url_fopen in my php configuration. BAM, worked! =) Thanks for the help everyone.


    I have a script that identifies country and IP and redirects page depending on country. It had worked in the past, I do not thing I had changed them but as of not $visitor_country is returning as null so it display as "unknown".

    The if else statements beloews are just a test for me to make sure it will do what I want it to do and it does. Since it returns as unknown I just used that to test.

    * I HAVE CHANGED THE CODE BELOW TO MY NEW CODE

    PHP Code:
    <?php 
    $ip 
    $_SERVER["REMOTE_ADDR"]; 
            if(
    filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)) 
                    
    $ip $_SERVER['HTTP_X_FORWARDED_FOR']; 
            if(
    filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)) 
                    
    $ip $_SERVER['HTTP_CLIENT_IP']; 
            
    $result = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" $ip)) 
                    ->
    geoplugin_countryName
            
    $visitor_country $result <> NULL $result "Unknown"
            
            
            if (
    $visitor_country == "Unknown") {
        
    ob_start(); // ensures anything dumped out will be caught

    // do stuff here
    $url 'http://staging.xxx.com'// this can be set based on whatever

    // clear out the output buffer
    while (ob_get_status()) 
    {
        
    ob_end_clean();
    }

    // no redirect
    header"Location: $url" );
    } else{
        echo 
    "HELLO";
    }
           
    ?>
    Last edited by 7daytheory; 06-04-2014 at 02:06 AM.

  • #2
    New Coder
    Join Date
    Jun 2005
    Location
    Blackpool. UK
    Posts
    98
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Chris

    Indifference will be the downfall of mankind, but who cares?

  • #3
    Regular Coder
    Join Date
    Aug 2013
    Posts
    149
    Thanks
    21
    Thanked 0 Times in 0 Posts
    Thanks, I definitely understand Switches but I guess I overthought it a bit? Either way handy link. Appreciate it

  • #4
    Regular Coder
    Join Date
    Aug 2013
    Posts
    149
    Thanks
    21
    Thanked 0 Times in 0 Posts
    I got the switch working but for some reason I get the IP Address but the country always comes up as uknown. Does anyone know why?

  • #5
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,224
    Thanks
    10
    Thanked 269 Times in 268 Posts
    Show us the new code. You say the country worked up on the example you have above, so it must be something you changed?

  • #6
    Regular Coder
    Join Date
    Aug 2013
    Posts
    149
    Thanks
    21
    Thanked 0 Times in 0 Posts
    I edited the code above, IM assuming its just getting a Null result so its just spitting out unknown. But I dont understand why.
    Last edited by 7daytheory; 06-03-2014 at 03:33 PM.

  • #7
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,224
    Thanks
    10
    Thanked 269 Times in 268 Posts
    It would have been handy to see your working and not working versions side by side, to see the difference. But I'd remove the "@" from the json_decode call, so you can see if it's returning any error messages.

  • #8
    Regular Coder
    Join Date
    Aug 2013
    Posts
    149
    Thanks
    21
    Thanked 0 Times in 0 Posts
    This code I had used a long time ago and I remember it giving me the country name, but when I first posted this I assumed it worked still but I was mistaken and it was returning unknown. I dont think I changed a thing since. The original code is just the code without the IF/Else statements. And it returns unknown.

    And I will try that thank you.

    Edit: I removed the @ and returning no error messages
    Last edited by 7daytheory; 06-03-2014 at 03:43 PM.

  • #9
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,224
    Thanks
    10
    Thanked 269 Times in 268 Posts
    So the ip address is good, and the call returns NULL with no error message? Sounds to me like the geoplugin service isn't working. I would contact them and see what's up.

  • #10
    Regular Coder
    Join Date
    Aug 2013
    Posts
    149
    Thanks
    21
    Thanked 0 Times in 0 Posts
    well it returns "unknown" , because the code is saying if its Null then the variable value is Unknown

  • #11
    Regular Coder
    Join Date
    Aug 2013
    Posts
    149
    Thanks
    21
    Thanked 0 Times in 0 Posts
    /Is there ways of doing this without a plugin that may go down. I don't want this to work sometimes and not 100% of the time

  • #12
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,224
    Thanks
    10
    Thanked 269 Times in 268 Posts
    Quote Originally Posted by 7daytheory View Post
    well it returns "unknown" , because the code is saying if its Null then the variable value is Unknown
    But I presume you've isolated this issue to their call to make sure you've done it right - just before the call you've printed out the URL you're sending (with the IP address in it) and just after the call you've printed out the return. Correct?

  • #13
    Regular Coder
    Join Date
    Aug 2013
    Posts
    149
    Thanks
    21
    Thanked 0 Times in 0 Posts
    I believe so.

    The variable $ip DOES contain the current IP address. SO Ive echoed that IP address and put it into http://www.geoplugin.net/json.gp?ip="[IP here] and it gives me ALL the information I need.

    EDIT: What Im doing is getting the IP address in the variable $ip, then $result contains the plugin and the link with the $ip variable at the end which when just put into the url gives me all the information I need. Then I selected the geoplugin_countryName which is the information I want and the $visitor_country = "result and if its null put Unknown.

    Again, to the best of my knowledge that is what I tried to do. I believe i did it correctly (obviously not though)
    Last edited by 7daytheory; 06-03-2014 at 04:12 PM.

  • #14
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,224
    Thanks
    10
    Thanked 269 Times in 268 Posts
    When I just put the url in my browser, it does appear to work. You might split your problem in half by printing out the result of file_get_contents and see if you get the right thing.

  • #15
    Regular Coder
    Join Date
    Aug 2013
    Posts
    149
    Thanks
    21
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tracknut View Post
    When I just put the url in my browser, it does appear to work. You might split your problem in half by printing out the result of file_get_contents and see if you get the right thing.
    The variable $result gives me Unkown as well, so the problem is in that line I guess.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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