View Full Version : GeoIP condition
steveneff
02-10-2012, 12:20 PM
I have GeoIP installed on my website (not in vbulletin) and want to use a condition based on country
I have put this code in forums/index.php
$efvip = $_SERVER['REMOTE_ADDR'];include('/home/myaccount/public_html/geo/geoip.inc');$gi = geoip_open('/home/myaccount/public_html/geo/ip/GeoIP.dat',GEOIP_STANDARD);$mygig = geoip_country_code_by_addr($gi, $efvip);$mycountry = array('AL','AZ');
In my forumdisplay template I have tried the following condition but the template gives an error in functions.
<vb:if condition="!in_array($mygig,$mycountry, true)">
Any ideas how I can get this to work?
Try adding this to your code in index.php:
vB_Template::preRegister('FORUMDISPLAY', array('mygig' => $mygig, 'mycountry' => $mycountry));
steveneff
02-10-2012, 01:14 PM
Still no go, when I try to add the condition in the template I get
Warning: Invalid argument supplied for foreach() in [path]/includes/functions.php on line 3416
Which version of vb are you running?
steveneff
02-10-2012, 01:25 PM
4.1.2 Patch Level 3
I also got a server error with your code
I also got a server error with your code
I tried it and there doesn't seem to be any errors in it, but it does need to be somewhere after global.php is included.
ETA: There is something else you could try instead of adding my code:
<vb:if condition="!in_array($GLOBALS['mygig'],$GLOBALS['mycountry'], true)">
steveneff
02-10-2012, 01:44 PM
Moving your first code down the page removed the server error but still not liking my condition
With that removed and using your condition I get
Could not find phrase ' The following template conditional expression contains function calls:
GLOBALS('mygig')
GLOBALS('mygig')
Oops - that's because I was being stupid. It should be $GLOBALS[] - I fixed the above code.
steveneff
02-10-2012, 02:06 PM
That clears the function calls but gives my original error
Warning: Invalid argument supplied for foreach() in [path]/includes/functions.php on line 3416
Hmm...and that error goes away when you remove the condtion?
steveneff
02-10-2012, 02:14 PM
yes it does.
I've tried it without show guest but still the same.
<vb:if condition="!in_array($GLOBALS['mygig'],$GLOBALS['mycountry'], true)">
<vb:if condition="$show['guest']"><div class="padtop">
stuff to go in here
</div></vb:if></vb:if>
OK, what happens if you try this:
<vb:if condition="!in_array('XX', array('AL', 'AZ'), true)">
<vb:if condition="$show['guest']"><div class="padtop">
stuff to go in here
</div></vb:if></vb:if>
or this:
<vb:if condition="!in_array('AL', array('AL', 'AZ'), true)">
<vb:if condition="$show['guest']"><div class="padtop">
stuff to go in here
</div></vb:if></vb:if>
steveneff
02-10-2012, 02:29 PM
They both parse, the second one does it's job ok
OK - if you still have the preRegister() call in there, what does this do:
<vb:if condition="!in_array($mygig, array('AL', 'AZ'), true)">
<vb:if condition="$show['guest']"><div class="padtop">
stuff to go in here
</div></vb:if></vb:if>
(I guess it will depend on if your country is one of AL or AZ - if it isn't, you could hard-code it in index.php).
steveneff
02-10-2012, 02:39 PM
parse OK but doesn't work, I changed AZ to GB so I can see if it works.
Well, OK, I don't know. Seems like maybe it's not seeing the variables for some reason.
If you only want to use that ip check in the forumdisplay template, maybe try moving your code to a plugin using hook forumdisplay_complete.
steveneff
02-10-2012, 03:06 PM
You mean this code
$efvip = $_SERVER['REMOTE_ADDR'];include('/home/myaccount/public_html/geo/geoip.inc');$gi = geoip_open('/home/myaccount/public_html/geo/ip/GeoIP.dat',GEOIP_STANDARD);$mygig = geoip_country_code_by_addr($gi, $efvip);$mycountry = array('AL','AZ','BA','BD','BG','BW','CD','EE','EG' ,'ET','FI','KW','KE','KP','KR','LB','LC','LR','LV' ,'MA','MT','MU','NG','NP','PH','RS','SK','SL','TN' ,'UY','VG','ZM','ZW');
Still no go, what a pig
Maybe this (assuming you don't use the country code elsewhere in the template): In a plugin using hook forumdisplay_complete:
$efvip = $_SERVER['REMOTE_ADDR'];
include('/home/myaccount/public_html/geo/geoip.inc');
$gi = geoip_open('/home/myaccount/public_html/geo/ip/GeoIP.dat',GEOIP_STANDARD);
$mygig = geoip_country_code_by_addr($gi, $efvip);
$mycountry = array('AL','AZ','BA','BD','BG','BW','CD','EE','EG' ,'ET','FI','KW','KE','KP','KR','LB','LC','LR','LV' ,'MA','MT','MU','NG','NP','PH','RS','SK','SL','TN' ,'UY','VG','ZM','ZW');
$show['in_country'] = $show['guest'] AND in_array($mygig, $mycountry);
then in the template:
<vb:if condition="$show[in_country]"><div class="padtop">
stuff to go in here
</div></vb:if>
steveneff
02-10-2012, 03:34 PM
No still nothing
Ah, OK...maybe I was wrong - maybe the code you're getting back from geoip_country_code_by_addr() isn't what you expect? What happens if you set $mygig to a country code? Also, you could put
{vb:raw GLOBALS.mygig}
in the template somewhere (not inside the if).
steveneff
02-10-2012, 09:04 PM
Sorry for the long delay, had a major brain mail function and wiped my forumhome template. Just got the thing back up and running.
I set $mygig to GBbut no change.
with raw globals in the template it displays my country code GB
--------------- Added 1328913211 at 1328913211 ---------------
I think it's working but not sure why.
hook
$efvip = $_SERVER['REMOTE_ADDR'];
include('/home/myaccount/public_html/geo/geoip.inc');
$gi = geoip_open('/home/myaccount/public_html/geo/ip/GeoIP.dat',GEOIP_STANDARD);
$mygig = geoip_country_code_by_addr($gi, $efvip);
$mycountry=array('AL','AZ','BA','BD','BG','BW','CD ','EE','EG','ET','FI','KW','KE','KP','KR','LB','LC ','LR','LV','MA','MT','MU','NG','NP','PH','RS','SK ','SL','TN','UY','VG','ZM','ZW');
$show['in_country'] = !in_array($mygig,$mycountry);
forumdisplay
<vb:if condition="$show['guest']">
<vb:if condition="$show[in_country]">
<div class="padtop">
stuff to go here
</div>
</vb:if>
</vb:if>
Yes, it does work.
Thank you
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.