一直都是用的 getenv()函数,但是,有的时候服务器上是用的较老版本的IIS ,就会获取失败或者获取错误的信息,所以本函数能完整的兼容,已经用到了项目中代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<span style="color: #000000;font-weight: bold">function</span> getIp<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
 
	<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/getenv" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">getenv</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"HTTP_CLIENT_IP"</span><span style="color: #009900">)</span> <span style="color: #339933">&&</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/strcasecmp" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strcasecmp</span></a><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/getenv" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">getenv</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"HTTP_CLIENT_IP"</span><span style="color: #009900">)</span><span style="color: #339933">,</span> <span style="color: #0000ff">"unknown"</span><span style="color: #009900">)</span><span style="color: #009900">)</span>
           <span style="color: #000088">$ip</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/getenv" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">getenv</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"HTTP_CLIENT_IP"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
       	<span style="color: #b1b100">else</span> <span style="color: #b1b100">if</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/getenv" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">getenv</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"HTTP_X_FORWARDED_FOR"</span><span style="color: #009900">)</span> <span style="color: #339933">&&</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/strcasecmp" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strcasecmp</span></a><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/getenv" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">getenv</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"HTTP_X_FORWARDED_FOR"</span><span style="color: #009900">)</span><span style="color: #339933">,</span> <span style="color: #0000ff">"unknown"</span><span style="color: #009900">)</span><span style="color: #009900">)</span>
           <span style="color: #000088">$ip</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/getenv" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">getenv</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"HTTP_X_FORWARDED_FOR"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
       	<span style="color: #b1b100">else</span> <span style="color: #b1b100">if</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/getenv" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">getenv</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"REMOTE_ADDR"</span><span style="color: #009900">)</span> <span style="color: #339933">&&</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/strcasecmp" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strcasecmp</span></a><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/getenv" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">getenv</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"REMOTE_ADDR"</span><span style="color: #009900">)</span><span style="color: #339933">,</span> <span style="color: #0000ff">"unknown"</span><span style="color: #009900">)</span><span style="color: #009900">)</span>
           <span style="color: #000088">$ip</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/getenv" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">getenv</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"REMOTE_ADDR"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
       	<span style="color: #b1b100">else</span> <span style="color: #b1b100">if</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/isset" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">isset</span></a><span style="color: #009900">(</span><span style="color: #000088">$_SERVER</span><span style="color: #009900">[</span><span style="color: #0000ff">'REMOTE_ADDR'</span><span style="color: #009900">]</span><span style="color: #009900">)</span> <span style="color: #339933">&&</span> <span style="color: #000088">$_SERVER</span><span style="color: #009900">[</span><span style="color: #0000ff">'REMOTE_ADDR'</span><span style="color: #009900">]</span>  <span style="color: #339933">&&</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/strcasecmp" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strcasecmp</span></a><span style="color: #009900">(</span><span style="color: #000088">$_SERVER</span><span style="color: #009900">[</span><span style="color: #0000ff">'REMOTE_ADDR'</span><span style="color: #009900">]</span><span style="color: #339933">,</span> <span style="color: #0000ff">"unknown"</span><span style="color: #009900">)</span><span style="color: #009900">)</span>
           <span style="color: #000088">$ip</span> <span style="color: #339933">=</span> <span style="color: #000088">$_SERVER</span><span style="color: #009900">[</span><span style="color: #0000ff">'REMOTE_ADDR'</span><span style="color: #009900">]</span><span style="color: #339933">;</span>
       	<span style="color: #b1b100">else</span>
           <span style="color: #000088">$ip</span> <span style="color: #339933">=</span> <span style="color: #0000ff">"unknown"</span><span style="color: #339933">;</span>
   		<span style="color: #b1b100">return</span><span style="color: #009900">(</span><span style="color: #000088">$ip</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>