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) {...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题