ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

fresh variables and php

May 01, 2010 5:46pm

Subscribe [3]
  • #1 / May 01, 2010 5:46pm

    I have a client who needs to have a site serve slightly different content based on region (US/UK are the only two, thankfully).

    Now, I looked into the ip to nation module, but thanks to some wonderful code oversight it uses (ip_address} as its only tag - which when used within the exp:weblog:entries tag shows the IP of the author NOT the user -  and won’t accept the alternate syntax.

    I’ve installed fresh variables, and am using the following code to return the user’s country (swiped from the interwebs, but it works okay).

    <?php
    function countryCityFromIP($ipAddr)
    {
       //function to find country and city name from IP address
      
      //verify the IP address for the  
      ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
      // This notice MUST stay intact for legal use
      $ipDetail=array(); //initialize a blank array
      //get the XML result from hostip.info
      $xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);
      //get the city name inside the node <gml:name> and </gml:name>
      preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);
      //assing the city name to the array
      $ipDetail['city']=$match[2]; 
      //get the country name inside the node <countryName> and </countryName>
      preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);
      //assign the country name to the $ipDetail array 
      $ipDetail['country']=$matches[1];
      //get the country name inside the node <countryName> and </countryName>
      preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
      $ipDetail['country_code']=$cc_match[1]; //assing the country code to array
      //return the array containing city, country and country code
      return $ipDetail;
    } 
      
    $IPDetail=countryCityFromIP($_SERVER['REMOTE_ADDR']); 
    echo $IPDetail['country_code']; //country of that IP address 
    ?>

    Which when used in fresh variables returns the short code for the user’s country nicely.

    However, using it in an {if} statement is apparently a dead end.

    All I want is for it to say

    {if user_country == "USA"}
    USA content
    {if:else}
    Rest of world
    {/if}

    Where (user_country} is the fresh variable containing the php, but currently no dice.

    Running 1.6.9 - it’s a fresh install so everything’s the most recent build.

    Any help would be appreciated! I’ve spent the whole day looking into solutions and if this can work it’ll be perfect….

  • #2 / May 01, 2010 6:07pm

    Greg Salt

    3988 posts

    Hi dasgrafik,

    This should work. What is the output of your conditional? Please change your template to check if anything is being returned at all:

    {if user_country == ''}
    No output at all
    {if:else}
    You are here: {user_country}
    {/if}

    Cheers

    Greg

  • #3 / May 01, 2010 8:41pm

    Hi Greg

    Running just the code you suggested I get

    You are here: UK

    And the URL for you to see what you’d get (if you’re located in another country!) is http://www.chrisdowson.co.uk/geolocation.

    I have to say I’m stumped. It returns a text string and by my understanding should compare that text string to whatever value I set… but doesn’t? It says UK but doesn’t see UK….

  • #4 / May 02, 2010 4:34pm

    Greg Salt

    3988 posts

    Hi dasgrafik,

    Let me check this out and get back to you.

    Cheers

    Greg

  • #5 / May 02, 2010 6:30pm

    Greg Salt

    3988 posts

    Hi dasgrafik,

    You can’t use code like this in Fresh Variables. You might consider turning this into a plugin or directly using PHP on your entry page.

    Cheers

    Greg

  • #6 / May 02, 2010 7:56pm

    Hi Greg, I’ve tried using it directly on the page with the same results. Because the PHP is parsed after the conditionals, apparently, it just doesn’t work - I’ve changed it to parse on input with the same effect.

    I’d love to make it a plugin but that’s well beyond my know-how.

  • #7 / May 02, 2010 8:01pm

    Hmmm, had a slight rethink.

    Could you go to http://www.chrisdowson.co.uk/geolocation/test and tell me what it says? If you’re outside the UK it should tell you so :o)

  • #8 / May 02, 2010 8:41pm

    Sue Crocker

    26054 posts

    Unfortunately it tells me I’m in the UK. I’m in Kansas City, Missouri. US 😊

  • #9 / May 03, 2010 7:33am

    Yeah, it’s weird as now I’ve got

    {if <?php
    function countryCityFromIP($ipAddr)
    {
       //function to find country and city name from IP address
      
      //verify the IP address for the  
      ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
      // This notice MUST stay intact for legal use
      $ipDetail=array(); //initialize a blank array
      //get the XML result from hostip.info
      $xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);
      //get the city name inside the node <gml:name> and </gml:name>
      preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);
      //assing the city name to the array
      $ipDetail['city']=$match[2]; 
      //get the country name inside the node <countryName> and </countryName>
      preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);
      //assign the country name to the $ipDetail array 
      $ipDetail['country']=$matches[1];
      //get the country name inside the node <countryName> and </countryName>
      preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
      $ipDetail['country_code']=$cc_match[1]; //assing the country code to array
      //return the array containing city, country and country code
      return $ipDetail;
    } 
      
    $IPDetail=countryCityFromIP($_SERVER['REMOTE_ADDR']); 
    echo $IPDetail['country_code']; //country of that IP address 
    ?> == UK}You are in the UK{if:else}You are outside the UK{/if}

    Which gives the opposite problem of apparently ALWAYS matching. Ah well…. if someone could write a geolocation plugin I think the community would be much better for it!

  • #10 / May 03, 2010 9:26am

    Sue Crocker

    26054 posts

    See:

    http://sim.plified.com/2009/07/08/get-location-information-via-visitor-ip-with-yahoo-query-language-yql-and-php/

    Just happened to be reading his blog this morning and spotted this.

    Does that help?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases