我使用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个回答



尝试更改... </ p>

  if(in_array($ jg_user_location,$ jg_event_locations)  ,TRUE)!== FALSE){... 
</ code> </ pre>

到此(删除额外的等号)...... </ p>

  if(in_array($ jg_user_location,$ jg_event_locations,TRUE)!= FALSE){... 
</ code> </ pre>
</ div>

展开原文

原文

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) {...

drq9991
drq9991 得到它了。 没意识到我必须那样做。 再次感谢。
一年多之前 回复
dongzhang7961
dongzhang7961 很高兴我能帮助你! 你能否接受我的答案作为你的解决方案? 如果您遇到其他问题,请告诉我。 祝其他项目好运!
一年多之前 回复
douqiao7958
douqiao7958 我觉得那很有效! 非常感谢!
一年多之前 回复
dongmeng1868
dongmeng1868 谢谢! 我会试试看。
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问