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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog