当前位置:首页 > ASP > 正文内容

网站获取真实IP地址的方法

canca14年前 (2011-11-13)ASP634

ASP篇

写了下asp取真实IP的代码,搭环境测试无代理、一级或多级代理的情况,可以正常获取

function   checkip(checkstring)'用正则判断IP是否合法
dim   re1
set   re1=new   RegExp
re1.pattern="^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"
re1.global=false
re1.Ignorecase=false
checkip=re1.test(checkstring)
set   re1=nothing
end   function


function   get_cli_ip()'取真实IP函数,先 HTTP_CLIENT_IP 再 HTTP_X_FORWARDED_FOR 再 REMOTE_ADDR
dim client_ip
if checkip(Request.ServerVariables("HTTP_CLIENT_IP"))=true then
         get_cli_ip = checkip(Request.ServerVariables("HTTP_CLIENT_IP"))
else
         MyArray = split(Request.ServerVariables("HTTP_X_FORWARDED_FOR"),",")
         if ubound(MyArray)>=0 then
                   client_ip = trim(MyArray(0))
                   if checkip(client_ip)=true then
                            get_cli_ip = client_ip
                            exit function
                   end if
         end if
         get_cli_ip = Request.ServerVariables("REMOTE_ADDR")
end if
end   function

---------------------------------------------------------------------

aspx vb.net篇

aspx vb.net获取真实IP的函数如下:

<script runat="server">
    Public Function CheckIp(ByVal ip As String) As Boolean
        Dim pat As String = "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"
        Dim reg As Regex = New Regex(pat)
        if ip = "" Then
            CheckIp = False
            exit Function
        end if
        CheckIp = reg.IsMatch(ip)
    End Function
   
    Public Function get_cli_ip() As String
        If ( Not(  System.Web.HttpContext.Current.Request.ServerVariables("HTTP_CLIENT_IP") Is Nothing) And CheckIp(System.Web.HttpContext.Current.Request.ServerVariables("HTTP_CLIENT_IP")) = True) Then
            get_cli_ip = System.Web.HttpContext.Current.Request.ServerVariables("HTTP_CLIENT_IP")
            Exit Function
        ElseIf Not(System.Web.HttpContext.Current.Request.ServerVariables("HTTP_X_FORWARDED_FOR") Is Nothing) Then
            Dim ips() As String = Split(System.Web.HttpContext.Current.Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ",")
            For i As Integer = 0 To ips.Length - 1
                If CheckIp(Trim(ips(i))) = True Then
                    get_cli_ip = Trim(ips(i))
                    Exit Function
                End If
            Next
        End If
        get_cli_ip = System.Web.HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")
    End Function
</script>

完整的测试页面:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Public Function CheckIp(ByVal ip As String) As Boolean
        Dim pat As String = "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"
        Dim reg As Regex = New Regex(pat)
        if ip = "" Then
            CheckIp = False
            exit Function
        end if
        CheckIp = reg.IsMatch(ip)
    End Function
   
    Public Function get_cli_ip() As String
        If ( Not(  System.Web.HttpContext.Current.Request.ServerVariables("HTTP_CLIENT_IP") Is Nothing) And CheckIp(System.Web.HttpContext.Current.Request.ServerVariables("HTTP_CLIENT_IP")) = True) Then
            get_cli_ip = System.Web.HttpContext.Current.Request.ServerVariables("HTTP_CLIENT_IP")
            Exit Function
        ElseIf Not(System.Web.HttpContext.Current.Request.ServerVariables("HTTP_X_FORWARDED_FOR") Is Nothing) Then
            Dim ips() As String = Split(System.Web.HttpContext.Current.Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ",")
            For i As Integer = 0 To ips.Length - 1
                If CheckIp(Trim(ips(i))) = True Then
                    get_cli_ip = Trim(ips(i))
                    Exit Function
                End If
            Next
        End If
        get_cli_ip = System.Web.HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")
    End Function
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
<% 
    Dim client_ip As String = get_cli_ip()
    System.Web.HttpContext.Current.Response.Write(client_ip)
 %>
</body>
</html>

------------------------------------------------------------------------

PHP篇

取真实IP的php代码
(discuz修改include/common.inc.php)
用以下这段代码:
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
        $onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),

'unknown')) {
        //使用CDN后判断真实ip by CDNUNION
       
        $testip = explode('.', getenv('HTTP_X_FORWARDED_FOR'));

        if ($testip[0]=='192' && $testip[1]=='168') {
                $onlineip = getenv('REMOTE_ADDR');
        }
    elseif($testip[0]=='10') {
                $onlineip = getenv('REMOTE_ADDR');
        }
        else {
                $onlineip = getenv('HTTP_X_FORWARDED_FOR');
        }

        //gamesir hack end} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'),

'unknown')) {
        $onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER

['REMOTE_ADDR'], 'unknown')) {
        //by Johnny
 $tmp_ip = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
 $tmp_ip1 = explode(',',$tmp_ip[0]);
 if ($tmp_ip1[0] == '192' && $tmp_ip1[1] == '168') {
  $onlineip = getenv('REMOTE_ADDR');
 }else if($tmp_ip1[0]=='10') {
                $onlineip = getenv('REMOTE_ADDR');
        }
        else{
                $onlineip = $tmp_ip[0];
        }
 unset($tmp_ip);unset($tmp_ip1);
 
}

替换这段代码:
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
 $onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),

'unknown')) {
 $onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
 $onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER

['REMOTE_ADDR'], 'unknown')) {
 $onlineip = $_SERVER['REMOTE_ADDR'];
}

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/203.html

标签: ASP
分享给朋友:

“网站获取真实IP地址的方法” 的相关文章

Asp包含文件include动态包含方法(含变量)

Author:flymorn Source:飘易博客Categories:Asp编程 PostTime:2007-12-18 12:24:04 正 文:     很多时候,由于程序设计需要,要求在asp的include包含文件里调用动态的文件。 如&l...

ASP生成JSON

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%><!--#include file="json.asp"--><!--#include file="inc/Conn.asp" --><...

ASP中如何实现类似Sleep()的时间延迟?

ASP中有时候也需要SLEEP()函数,即等待一段时间再继续执行跟着的代码。如何实现呢?我搜索了很久,得到很少解决的办法。下面是一些折中的做法: 1. sub MySleep(mySleepTime)dim myTimemyTime = nowWhile DateDiff("s",myTime,no...

CCKiller v2.0 ASP版发布

CCKiller  v2.0 是CC攻击的杀手,能防CC攻击,DDOS攻击,防采集,适合于所有ASP网站!! 使用很简单,直接include进来就可以了。无需数据库支持。性能极优,效果极佳!!   让CPU回归自然的波浪线吧!!!   如需要,请联系:canc...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。