Home >php教程 >php手册 >php中获取真实IP地址与IP地址验证函数

php中获取真实IP地址与IP地址验证函数

WBOY
WBOYOriginal
2016-05-25 16:56:191036browse
本文章就介绍二个函数一个是验证IP地址,另一个是获取用户的真实IP地址的函数,也是常用的两个IP操作函数,有需要的同学可以参考一下。

获取用户真实IP地址

 代码如下 复制代码

function get_client_ip( )
{
        if ( getenv( "REMOTE_ADDR" ) && strcasecmp( getenv( "REMOTE_ADDR" ), "unknown" ) )
        {
                $onlineip = getenv( "REMOTE_ADDR" );
                return $onlineip;
        }
        if ( isset( $_SERVER['REMOTE_ADDR'] ) && $_SERVER['REMOTE_ADDR'] && strcasecmp( $_SERVER['REMOTE_ADDR'], "unknown" ) )
        {
                $onlineip = $_SERVER['REMOTE_ADDR'];
                return $onlineip;
        }
        if ( getenv( "HTTP_CLIENT_IP" ) && strcasecmp( getenv( "HTTP_CLIENT_IP" ), "unknown" ) )
        {
                $onlineip = getenv( "HTTP_CLIENT_IP" );
                return $onlineip;
        }
        if ( getenv( "HTTP_X_FORWARDED_FOR" ) && strcasecmp( getenv( "HTTP_X_FORWARDED_FOR" ), "unknown" ) )
        {
                $onlineip = getenv( "HTTP_X_FORWARDED_FOR" );
        }
        return $onlineip;
}

判断是否为IP地址

 代码如下 复制代码


function is_ip( $IP )
{
        $IP_ARRAY = explode( ".", $IP );
        $IP_ARRAY_NUM = sizeof( $IP_ARRAY );
        if ( $IP_ARRAY_NUM != 4 )
        {
                return FALSE;
        }
        $I = 0;
        for ( ;    $I         {
                if ( !is_numeric( $IP_ARRAY[$I] ) && $IP_ARRAY[$I]                 {
                        return FALSE;
                }
                if ( !( $I == 3 ) && !( $IP_ARRAY[$I] == 255 ) )
                {
                        continue;
                }
                return FALSE;
        }
        return TRUE;
}



永久链接:

转载随意!带上文章地址吧。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn