I want to run a js file. Currently i am using this code
addScript("http://domain.com/file.js");
but i want some code like which do execute this js file country wise some thing like this
http://j.maxmind.com/app/geoip.js
var country= geoip_country_code();
if(country == "GB" )
You want to serve up a .js, generated by PHP, whose content is dependent on the country from which the request comes?
If so, you can use the GeoIP extension for PHP: http://www.maxmind.com/app/php
In addition to Jack's answer (+1), here's some code that should work if you use the GeoIP PHP module:
addScript("http://domain.com/javascriptgenerator.php");
Then in javascriptgenerator.php
, you simply do:
<?php
header("Content-type: application/javascript"); //Tell the browser we're sending JS
require_once "Net/GeoIP.php"; //Path to GeoIP PHP module
$geoip = Net_GeoIP::getInstance("/path/to/geoipdb.dat");
try {
switch ($geoip->lookupCountryCode($_SERVER['REMOTE_ADDR'])) {
case "CA":
//Generate JS for Canadian users
break;
case "FR":
//Generate JS for French users
break;
//Any number of case statements goes here
default:
//Show default JS code
}
} catch (Exception $e) {
//Handle exception
//You probably want to show the default JS code if the geo-location is unsuccesful
}
?>
See also http://pear.php.net/manual/en/package.networking.net-geoip.lookupcountrycode.php