dongtidai6519 2018-10-23 19:46
浏览 84
已采纳

我使用GeoLite City数据库识别用户位置的PHP代码只是部分工作。 有人可以看看吗?

I have simple php code to show ads in certain geo locations (US regions) and it works for some states but not for others. Can anyone take a look at my code & spot errors?

This code below is what I'm using to try to get US state location from visitors and show them particular ads if they are from those states. This is on an nginx server and the host already set up separate buckets for the cache so that certain US states (the ones identified below) would have different caches. I have cleared the server cache. The midwestern location works (Tennessee & Kentucky) but the east coast location does not work (DC). Can anyone give any tips on my php? I would so appreciate it. The database is MaxMind GeoLite City database & therefore the regions/US states data should be available.

The Wordpress framework is beans and uses the "beans smart action."

<?php    
beans_add_smart_action('beans_content_prepend_markup', 'top_geo_home_ad', 5);

function top_geo_home_ad() {
$jg_user_location = getenv('GEOIP_REGION_NAME');
$jg_event_locations = array('Massachusetts', 'District of Columbia', 'Tennessee', 'Kentucky', 'New York', 'Maryland', 'DC',  'Delaware', 'Pennsylvania', 'Virginia');
if ($jg_user_location == 'District of Columbia' || $jg_user_location == 'New York' || $jg_user_location == 'Maryland' || $jg_user_location == 'Massachusetts' || $jg_user_location == 'Delaware' || $jg_user_location == 'Pennsylvania' || $jg_user_location == 'Virginia') {
    $jg_user_location = 'DC'; 
} elseif ($jg_user_location == 'Kentucky') { 
    $jg_user_location = 'Tennessee';
}
if(in_array( $jg_user_location, $jg_event_locations, TRUE )!==FALSE) {
    beans_add_smart_action( 'beans_content_prepend_markup', 'CEO_geoad_' . $jg_user_location, 6 );
    }
else {
    beans_add_smart_action( 'beans_content_prepend_markup', 'jg_normal_top_ad', 6);
}
}


function CEO_geoad_DC() {
    echo '<div class="ad-wrap horiz">' . do_shortcode('[aas_zone zone_id="56461"]') . '</div>';
}

function CEO_geoad_Tennessee() {
    echo '<div class="ad-wrap horiz">' . do_shortcode('[aas_zone zone_id="56470"]') . '</div>';
}

function jg_normal_top_ad() {
    echo '<div class="ad-wrap horiz">' . do_shortcode('[aas_zone zone_id="49497"]') . '</div>';
}
?>
  • 写回答

1条回答 默认 最新

  • dtry54612 2018-10-23 20:21
    关注

    Try changing...

    if(in_array( $jg_user_location, $jg_event_locations, TRUE )!==FALSE) {...
    

    to this (remove extra equal sign)...

    if(in_array( $jg_user_location, $jg_event_locations, TRUE )!=FALSE) {...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?