Including geotargeting script.

Permalink 1 user found helpful
Hi
I'm trying to install a php script from MindMax.

Code;
if (isset($_COOKIE["geoip"])) {
$country = $_COOKIE['geoip'];
$GLOBALS['geoip'] = $country;
}
else
{
include("/var/www/vhosts/xxxxxxx.com/httpdocs/tools/GeoIP/geoip.php");
$gi = geoip_open("/var/www/vhosts/xxxxxxx.com/httpdocs/tools/GeoIP/GeoIP-106_20120327.dat",GEOIP_STANDARD);
$country = geoip_country_code_by_addr($gi, $REMOTE_ADDR);
$GLOBALS['geoip'] = $country;
geoip_close($gi);

setcookie("geoip", $country, time()+3600, "/", ".xxxxxxx.com", 0); //1 hour cookie

}

Where should I put it to allow access to the var $country from within templates.
I've installed the PHP code add on and then created a PHP code block with a page with the following in it.

echo $country;
and
echo $GLOBALS['geoip'];

but the Country is not showing.

Could any one advice what im doing wrong?


Thanks
Kevin

 
jordanlev replied on at Permalink Reply
jordanlev
Sounds like you need this to run before every page in your site. If you just put it in a block it will only be displayed on pages having that block (and also, depending on where the code is that *uses* the geoip, it might be running before the block code runs so it's not available yet).

I think your best bet is to put this in an event.

1) Create a new file in your site's "config" directory, called "site_events.php".
2) Add this code to the site_events.php file:
<?php defined('C5_EXECUTE') or die("Access Denied.");
Events::extend('on_start', 'GeoIp', 'on_start', 'libraries/geo_ip.php'); ?>

3) Now create a new file in your site's top-level "libraries" directory, called "geo_ip.php".

4) Add this code to the geo_ip.php file:
<?php defined('C5_EXECUTE') or die("Access Denied.");
class GeoIp {
   function on_start() {
      //PASTE ALL OF YOUR CODE HERE IN THIS FUNCTION
   }
}

5) Paste all of your code into that function where I put the comment.

You can test that the function is actually being called by putting "die('test geoip function called');" in there and seeing if you just get a blank white page with that message in it. If it's getting called but it's still not working, then there's a problem with your code.

Hope that helps.

-Jordan
kevinbrown04 replied on at Permalink Reply
Thanks Jorden for your help.
I created the following file like you suggested.
How do i read the variable $country from a php call in a template? (im using the PHP code add on to try of echo $country; but its throwing an error.
I'm really new to this so sorry if its a dumb question.

<?php defined('C5_EXECUTE') or die("Access Denied.");
class GeoIp {
   function on_start() {
      //PASTE ALL OF YOUR CODE HERE IN THIS FUNCTION
if (isset($_COOKIE["geoip"])) {
$country = $_COOKIE['geoip'];
return $country;
} else {
include("/var/www/vhosts/aussiefloyd.com/httpdocs/libraries/geoip.inc.php");
$gi = geoip_open("/var/www/vhosts/aussiefloyd.com/httpdocs/libraries/GeoIP.dat",GEOIP_STANDARD);
$country = geoip_country_code_by_addr($gi, $REMOTE_ADDR);
geoip_close($gi);
setcookie("geoip", $country, time()+3600, "/", ".aussiefloyd.com", 0); //1 hour cookie
return $country;
}
jordanlev replied on at Permalink Reply
jordanlev
You took out the "$GLOBALS['geoip'] = $country" line in your new code. I think using that will be the easiest way to achieve what you want. Otherwise it will be really complicated to figure out which script has access to which variables (because you are combining external scripts with C5).

Hope that makes sense.

-Jordan