已掉线,重新登录

首页 > 绿虎论坛 > 历史版块 > 编程 > PHP > 源码

标题: [转]php得到真实IP 

作者: @Ta

时间: 2013-01-26

点击: 3243

<?php
function i2c_realip ( )
{
// No IP found (will be overwritten by for
// if any IP is found behind a firewall)
$ip = FALSE ;
// If HTTP_CLIENT_IP is set, then give it priority
if ( ! empty ( $_SERVER [ "HTTP_CLIENT_IP" ] ) ) {
$ip = $_SERVER [ "HTTP_CLIENT_IP" ] ;
}
// User is behind a proxy and check that we discard RFC1918 IP addresses
// if they are behind a proxy then only figure out which IP belongs to the
// user. Might not need any more hackin if there is a squid reverse proxy
// infront of apache.
if ( ! empty ( $_SERVER [ 'HTTP_X_FORWARDED_FOR' ] ) ) {
// Put the IP's into an array which we shall work with shortly.
$ips = explode ( ", " , $_SERVER [ 'HTTP_X_FORWARDED_FOR' ] ) ;
if ( $ip ) { array_unshift ( $ips , $ip ) ; $ip = FALSE ; }
for ( $i = 0 ; $i < count ( $ips ) ; $i ++ ) {
// Skip RFC 1918 IP's 10.0.0.0/8, 172.16.0.0/12and
// 192.168.0.0/16
if ( ! preg_match ( '/^(?:10|172.(?:1[6-9]|2d|3[01])|192.168)./' , $ips [ $i ] ) ) {
if ( version_compare ( phpversion ( ) , "5.0.0" , ">=" ) ) {
if ( ip2long ( $ips [ $i ] ) != false ) {
$ip = $ips [ $i ] ;
break ;
}
} else {
if ( ip2long ( $ips [ $i ] ) != - 1 ) {
$ip = $ips [ $i ] ;
break ;
}
}
}
}
}
// Return with the found IP or the remote address
return ( $ip ? $ip : $_SERVER [ 'REMOTE_ADDR' ] ) ;
}
echo i2c_realip ( ) ;
?>

[隐藏样式|查看源码]


『回复列表(8|隐藏机器人聊天)』

2. remote_addr才是真理
(/@Ta/2013-01-26 22:05//)

3. 不懂
(/@Ta/2013-01-26 22:14//)

4. @Bewind 求例子
(/@Ta/2013-01-26 22:30//)

5. REMOTE_ADDR得到的是来源IP,非真实
(/@Ta/2013-01-26 22:36//)

6. 不懂、
(/@Ta/2013-01-26 22:40//)

7. @浪人  那请问如何伪装remote_addr
(/@Ta/2013-01-27 00:25//)

8. @Bewind 你可以试试。我也不知道。
(/@Ta/2013-01-27 07:33//)

9. @浪人  php 只能伪装头信息来模拟ip ,而addr不是根据header获取的
(/@Ta/2013-01-27 11:27//)

回复需要登录

9月16日 17:51 星期二

本站由hu60wap6驱动

备案号: 京ICP备18041936号-1