IP を使用して訪問ユーザーがいる都市を判断する PHP コード

WBOY
リリース: 2016-07-25 08:42:37
オリジナル
963 人が閲覧しました
是在、哪个城市
  1. function detect_city($ip) {
  2. $default = 'UNKNOWN';
  3. $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/ 20100115 Firefox/3.6 (.NET CLR 3.5.30729)';
  4. $url = 'http://ipinfodb.com/ip_locator.php?ip=' 。 urlencode($ip);
  5. $ch =curl_init();
  6. $curl_opt = array(
  7. CURLOPT_FOLLOWLOCATION => 1,
  8. CURLOPT_HEADER => 0,
  9. CURLOPT_RETURNTRANSFER => 1,
  10. CURLOPT_USERAGENT =&g $curlopt_useragent、
  11. CURLOPT_URL => $url,
  12. CURLOPT_TIMEOUT => 1,
  13. CURLOPT_REFERER => $_SERVER['HTTP_HOST'],
  14. );
  15. curl_setopt_array($ch, $curl_opt);
  16. $content =curl_exec($ch);
  17. if (!is_null($curl_info)) {
  18. $curl_info =curl_getinfo($ch);
  19. }
  20. curl_close($ch);
  21. if ( preg_match('{
  22. City : ([^<]*)
  23. }i', $content, $regs) ) {
  24. $city = $regs[1];
  25. }
  26. if ( preg_match('{
  27. 州/県 : ([^<]*)
  28. }i', $content, $regs) ) {
  29. $state = $regs[1];
  30. }
  31. if( $ city!='' && $state!='' ){
  32. $location = $city . 「、」。 $state;
  33. return $location;
  34. }else{
  35. return $default;
  36. }
  37. }
复制代
使用法:

$ip = $_SERVER['REMOTE_ADDR'];
  • $city = detect_city($ip);
  • echo $city;
  • ?>
  • 复制發
  • ソース:php.cn
    このウェブサイトの声明
    この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
    人気のチュートリアル
    詳細>
    最新のダウンロード
    詳細>
    ウェブエフェクト
    公式サイト
    サイト素材
    フロントエンドテンプレート
    私たちについて 免責事項 Sitemap
    PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!