2018-10-23 19:46 阅读 72

我使用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."

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 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) {...
    点赞 评论 复制链接分享