ユーザーの IP アドレスに基づいてユーザーがいる都市を特定する必要があるという需要があります。
この記事はドキュメンタリー記事であり、論理性はありません。そのようなニーズを持つ友人は、それをコピーして直接使用できます。詳細を説明する必要はなく、コードに直接アクセスできます。
<code><span><?php</span> header(<span>'Content-Type:text/html;Charset=utf-8'</span>); <span><span>function</span><span>GetIp</span><span>()</span>{</span><span>$realip</span> = <span>''</span>; <span>$unknown</span> = <span>'unknown'</span>; <span>if</span> (<span>isset</span>(<span>$_SERVER</span>)){ <span>if</span>(<span>isset</span>(<span>$_SERVER</span>[<span>'HTTP_X_FORWARDED_FOR'</span>]) && !<span>empty</span>(<span>$_SERVER</span>[<span>'HTTP_X_FORWARDED_FOR'</span>]) && strcasecmp(<span>$_SERVER</span>[<span>'HTTP_X_FORWARDED_FOR'</span>], <span>$unknown</span>)){ <span>$arr</span> = explode(<span>','</span>, <span>$_SERVER</span>[<span>'HTTP_X_FORWARDED_FOR'</span>]); <span>foreach</span>(<span>$arr</span><span>as</span><span>$ip</span>){ <span>$ip</span> = trim(<span>$ip</span>); <span>if</span> (<span>$ip</span> != <span>'unknown'</span>){ <span>$realip</span> = <span>$ip</span>; <span>break</span>; } } }<span>else</span><span>if</span>(<span>isset</span>(<span>$_SERVER</span>[<span>'HTTP_CLIENT_IP'</span>]) && !<span>empty</span>(<span>$_SERVER</span>[<span>'HTTP_CLIENT_IP'</span>]) && strcasecmp(<span>$_SERVER</span>[<span>'HTTP_CLIENT_IP'</span>], <span>$unknown</span>)){ <span>$realip</span> = <span>$_SERVER</span>[<span>'HTTP_CLIENT_IP'</span>]; }<span>else</span><span>if</span>(<span>isset</span>(<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>]) && !<span>empty</span>(<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>]) && strcasecmp(<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>], <span>$unknown</span>)){ <span>$realip</span> = <span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>]; }<span>else</span>{ <span>$realip</span> = <span>$unknown</span>; } }<span>else</span>{ <span>if</span>(getenv(<span>'HTTP_X_FORWARDED_FOR'</span>) && strcasecmp(getenv(<span>'HTTP_X_FORWARDED_FOR'</span>), <span>$unknown</span>)){ <span>$realip</span> = getenv(<span>"HTTP_X_FORWARDED_FOR"</span>); }<span>else</span><span>if</span>(getenv(<span>'HTTP_CLIENT_IP'</span>) && strcasecmp(getenv(<span>'HTTP_CLIENT_IP'</span>), <span>$unknown</span>)){ <span>$realip</span> = getenv(<span>"HTTP_CLIENT_IP"</span>); }<span>else</span><span>if</span>(getenv(<span>'REMOTE_ADDR'</span>) && strcasecmp(getenv(<span>'REMOTE_ADDR'</span>), <span>$unknown</span>)){ <span>$realip</span> = getenv(<span>"REMOTE_ADDR"</span>); }<span>else</span>{ <span>$realip</span> = <span>$unknown</span>; } } <span>$realip</span> = preg_match(<span>"/[\d\.]{7,15}/"</span>, <span>$realip</span>, <span>$matches</span>) ? <span>$matches</span>[<span>0</span>] : <span>$unknown</span>; <span>return</span><span>$realip</span>; } <span><span>function</span><span>GetIpLookup</span><span>(<span>$ip</span> = <span>''</span>)</span>{</span><span>if</span>(<span>empty</span>(<span>$ip</span>)){ <span>$ip</span> = GetIp(); } <span>$res</span> = @file_get_contents(<span>'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip='</span> . <span>$ip</span>); <span>if</span>(<span>empty</span>(<span>$res</span>)){ <span>return</span><span>false</span>; } <span>$jsonMatches</span> = <span>array</span>(); preg_match(<span>'#\{.+?\}#'</span>, <span>$res</span>, <span>$jsonMatches</span>); <span>if</span>(!<span>isset</span>(<span>$jsonMatches</span>[<span>0</span>])){ <span>return</span><span>false</span>; } <span>$json</span> = json_decode(<span>$jsonMatches</span>[<span>0</span>], <span>true</span>); <span>if</span>(<span>isset</span>(<span>$json</span>[<span>'ret'</span>]) && <span>$json</span>[<span>'ret'</span>] == <span>1</span>){ <span>$json</span>[<span>'ip'</span>] = <span>$ip</span>; <span>unset</span>(<span>$json</span>[<span>'ret'</span>]); }<span>else</span>{ <span>return</span><span>false</span>; } <span>return</span><span>$json</span>; } <span>$ipInfos</span> = GetIpLookup(<span>'123.125.114.144'</span>); <span>//baidu.com IP地址</span> var_dump(<span>$ipInfos</span>);</code>
Sina のオープン API を使用すると、実行速度は依然として非常に優れています。 この記事は CSDN からのものです。転載する場合は出典を明記してください。 http://blog.csdn.net/liruxing1715/article/details/28238483
').addClass('事前番号付け').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });上記は、IP アドレスと PHP コンテンツを含めて、IP アドレスに基づいて都市を取得する PHP を紹介しました。PHP チュートリアルに興味のある友人に役立つことを願っています。