我想运行一个js文件。目前我正在使用此代码
addScript("http://domain.com/file.js");
但是我想要一些代码,例如执行此JS文件的国家/地区,例如这样的事情
http://j.maxmind.com/app/geoip.js
var 国家 = geoip_country_code();
if(国家 == "GB" )
您想提供一个由 PHP 生成的.js,其内容取决于请求来自哪个国家/地区?
如果是这样,您可以使用 PHP 的 GeoIP 扩展:http://www.maxmind.com/app/php
除了Jack的答案(+1)之外,如果您使用GeoIP PHP模块,这里有一些代码应该可以工作:
addScript("http://domain.com/javascriptgenerator.php");
那么在javascriptgenerator.php
中,您只需执行以下操作:
<?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
}
?>
另请参阅 http://pear.php.net/manual/en/package.networking.net-geoip.lookupcountrycode.php