目前我在下面显示了此代码,假设当用户国家/地区=到美国但id无法正常工作时显示广告。
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
$location = $details->country;
$details->country;
if($location == 'CA') {
echo '<script type="text/javascript">';
echo "<!-- window['pubs_ad_padid']"; echo '= "1005";';
echo "window['pubs_ad_subid'] = ''; //Insert value to pass through your sub id //-->";
echo "var se = document.createElement('script');";
echo 'se.type = "text/javascript";';
echo "se.src = (document.location.protocol ==="; echo ' "https:" ? "https:" : "http:") + "//d.example.com/js/pub.js";';
echo "document.getElementsByTagName('head')[0].appendChild(se);";
echo "</script>";
}
这是不包含所有回声爵士乐的正常代码。
<script type="text/javascript">
<!--
window['pubs_ad_padid'] = "1005";
window['pubs_ad_subid'] = ''; //Insert value to pass through your sub id
//-->
var se = document.createElement('script');
se.type = "text/javascript";
se.src = (document.location.protocol === "https:" ? "https:" : "http:") + "//d.example.com/js/pub.js";
document.getElementsByTagName('head')[0].appendChild(se);
</script>
你有没有这样试过:
<?php
if($location == 'CA') {
?>
<script type="text/javascript">
<!--
window['pubs_ad_padid'] = "1005";
window['pubs_ad_subid'] = ''; //Insert value to pass through your sub id
//-->
var se = document.createElement('script');
se.type = "text/javascript";
se.src = (document.location.protocol === "https:" ? "https:" : "http:") + "//d.example.com/js/pub.js";
document.getElementsByTagName('head')[0].appendChild(se);
</script>
<?>}</?>
这就是它的基础。 这将做的只是在满足条件时回显该脚本。 如果需要,它还允许您更轻松地在其中添加另一个PHP回显。 而且您不必摆弄所有这些报价。 这可能是您的问题所在。