最近模板兔给客户做了一个功能就是对海外用户价格翻倍,那么我们如何判断是海外用户呢?
这里,我们可以通过一个IP获取归属地的接口来简单判断下,但是这样判断可能会导致网络卡顿。
$ip = mbt_get_ip(); $api = "http://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=".$ip."&co=&resource_id=6006"; $result = get_url_contents($api); $result= iconv('GB2312', 'UTF-8', $result); $result = json_decode($result,true); if($result['status'] == '0'){ $loct = $result['data'][0]['location']; //echo $loct; if(strpos($loct,'北京')!==false || strpos($loct,'上海')!==false || strpos($loct,'天津')!==false || strpos($loct,'重庆')!==false || strpos($loct,'内蒙古')!==false || strpos($loct,'广西')!==false || strpos($loct,'西藏')!==false || strpos($loct,'宁夏')!==false || strpos($loct,'新疆')!==false || strpos($loct,'河北省')!==false || strpos($loct,'山西省')!==false || strpos($loct,'辽宁省')!==false || strpos($loct,'吉林省')!==false || strpos($loct,'黑龙江省')!==false || strpos($loct,'江苏省')!==false || strpos($loct,'浙江省')!==false || strpos($loct,'安徽省')!==false || strpos($loct,'福建省')!==false || strpos($loct,'江西省')!==false || strpos($loct,'山东省')!==false || strpos($loct,'河南省')!==false || strpos($loct,'湖北省')!==false || strpos($loct,'湖南省')!==false || strpos($loct,'广东省')!==false || strpos($loct,'海南省')!==false || strpos($loct,'四川省')!==false || strpos($loct,'贵州省')!==false || strpos($loct,'云南省')!==false || strpos($loct,'陕西省')!==false || strpos($loct,'甘肃省')!==false || strpos($loct,'青海省')!==false || strpos($loct,'台湾')!==false || strpos($loct,'香港')!==false || strpos($loct,'澳门')!==false){ }else{ $price = $price*2; } }
当然,还可以通过本地IP库的方法来判断,这里就不多赘述了。
0 个评论