The following code:
{exp:ip_to_nation:world_flags type="text"}{ip_address}{/exp:ip_to_nation:world_flags}...outputs nothing.
{ip_address} does output a correct IP address on its own.
Wondering what might be wrong?
This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.
The active forums are here.
June 13, 2012 11:33am
Subscribe [4]#1 / Jun 13, 2012 11:33am
The following code:
{exp:ip_to_nation:world_flags type="text"}{ip_address}{/exp:ip_to_nation:world_flags}...outputs nothing.
{ip_address} does output a correct IP address on its own.
Wondering what might be wrong?
#2 / Jun 13, 2012 7:13pm
Hey gwarek,
I am sorry to hear you are running into this problem.
What version of ExpressionEngine are you currently running?
What version of IP2Nation?
Please let me know!
Cheers,
#3 / Jun 13, 2012 10:31pm
Hi gwarek,
This is a problem of order…EE2 Parse Order to be precise. {ip_address} is one of a list of standard global template variables that get parsed last in the EE2 parse order. Module and plugin tags get parsed earlier so the problem here is that EE is parsing the {exp:ip_to_nation:world_flags} tag before it parses the {ip_address} variable, effectively invoking the world_flags method of the Ip_to_nation module class with an unparsed {ip_address} so when Ip_to_nation attempts to make the standard $this->EE->TMPL->tagdata call to get the IP address, the value is the string ”{ip_address}”, since at that point, {ip_address} has not yet been parsed and converted to the user’s IP address.
See this document for a more thorough overview of EE2’s parsing order.
#4 / Jun 14, 2012 1:02am
Just jumping in here out of curiosity…
So the solution would be to enable PHP at input parsing stage and echo out the ip address using $_SERVER[‘remote_addr’]?
*edit*
Thanks for sharing the link - that looks like a very useful thing to know!
#5 / Jun 14, 2012 1:49am
@DigitalDoctors - Yes a solution would be to enable PHP at input parsing stage but instead of echoing out $_SERVER[‘remote_addr’] I would recommend echoing $this->EE->session->userdata(‘ip_address’) instead since this always returns the same value as the {ip_address} global variable.
#6 / Jun 14, 2012 6:03am
I’m on 2.5.2 and IP2Nation 3.0.
Using PHP does work, thanks for the replies. If the tag doesn’t function due to parse order it might be good to mention this on the ip2nation documentation, as it’s an example there.
Has anyone successfully used ip2nation to display different content depending on country?
I used to do this with a direct query to the ip2nation table, but with the upgrade to ipv6 and the new way of storing IP addresses, this (understandably) doesn’t work anymore.
#7 / Jun 15, 2012 1:29pm
Hey All,
Love the interaction here, nice work! Thanks for the input Owan.
Is there anything else I can help you with gwarek?
Cheers,
#8 / Jun 18, 2012 12:54pm
If you’re using a manual ip lookup for geolocation (e.g. similar to this example), this will break with EE 2.5.2 ipv6 support.
Here’s how I got it to work again - thought I’d share!
$ip = $this->EE->session->userdata('ip_address');
$ip = ip2long($ip);
$ip = base_convert($ip, 10, 16);
$ip = str_pad($ip, 32, "0", STR_PAD_LEFT);
$sql = "select country from exp_ip2nation where unhex('" . $ip. "') between ip_range_low and ip_range_high";
list($countryName) = mysql_fetch_row(mysql_query($sql));#9 / Jun 18, 2012 4:54pm
Hey gwarek,
Awesome!
If you need anything else, please just let me know by opening a new thread.
Cheers,