使用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 => $curlopt_useragent,
  11. CURLOPT_URL => $url,
  12. CURLOPT_TIMEOUT => 1,
  13. CURLOPT_REFERER => 'http://' . $_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 : ([^}i', $content, $regs) ) {
  23. $city = $regs[1];
  24. }
  25. if ( preg_match('{
  26. State/Province : ([^}i', $content, $regs) ) {
  27. $state = $regs[1];
  28. }
  29. if( $city!='' && $state!='' ){
  30. $location = $city . ', ' . $state;
  31. return $location;
  32. }else{
  33. return $default;
  34. }
  35. }
复制代码

用法:

  1. $ip = $_SERVER['REMOTE_ADDR'];
  2. $city = detect_city($ip);
  3. echo $city;
  4. ?>
复制代码

是在, 哪个城市


원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!