asp.net(vb.net)獲取真實(shí)IP的函數(shù)
更新時(shí)間:2010年11月03日 13:05:52 作者:
asp.net(vb.net)獲取真實(shí)IP的函數(shù),需要的朋友可以參考下。
aspx vb.net獲取真實(shí)IP的函數(shù)如下:
<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>
完整的測(cè)試頁(yè)面:
<%@ 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>
復(fù)制代碼 代碼如下:
<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>
完整的測(cè)試頁(yè)面:
復(fù)制代碼 代碼如下:
<%@ 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>
您可能感興趣的文章:
- VB.net常用字符和日期等函數(shù)
- vb.net讀寫(xiě)文件代碼
- vb.net發(fā)布水晶報(bào)表程序步驟
- VB.net 查詢獲取數(shù)據(jù)庫(kù)數(shù)據(jù)信息
- VB.net讀取Word文檔屬性的方法
- VB.NET拷貝整個(gè)目錄下所有子目錄及文件的實(shí)例代碼
- VB.NET進(jìn)度條的方法代碼
- 獲取客戶端IP地址c#/vb.net各自實(shí)現(xiàn)代碼
- asp.net 驗(yàn)證碼的簡(jiǎn)單制作(vb.net+C#)
- 在VB.NET應(yīng)用中使用MySQL的方法
- vb.net操作注冊(cè)表的方法分析【增加,修改,刪除,查詢】
相關(guān)文章
asp.net下UTF-7轉(zhuǎn)GB2312編碼的代碼(中文)
UTF-7轉(zhuǎn)換GB2312編碼的方法2010-07-07
ASP.NET MVC API 接口驗(yàn)證的示例代碼
本篇文章主要介紹了ASP.NET MVC API 接口驗(yàn)證的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
ASP.NET MVC3 SEO優(yōu)化:利用Routing特性提高站點(diǎn)權(quán)重
這篇文章主要介紹了ASP.NET MVC3 SEO優(yōu)化:利用Routing特性消除多個(gè)路徑指向同一個(gè)Action,從而提高站點(diǎn)權(quán)重,需要的朋友可以參考下。2016-06-06
asp.net(c#)下Jmai去說(shuō)明 使用與下載
asp.net(c#)下Jmai去說(shuō)明 使用與下載...2007-06-06
asp.net 一個(gè)封裝比較完整的FTP類(lèi)
.NET一個(gè)FTP操作類(lèi)。但這個(gè)類(lèi)經(jīng)常出錯(cuò),使用DISCUZ使用的類(lèi)就好多了。2009-06-06
.NET?6中為record類(lèi)型自定義Equals方法
這篇文章主要介紹了.NET?6中為record類(lèi)型自定義Equals方法,record類(lèi)型,這是一種新引用類(lèi)型,而不是類(lèi)或結(jié)構(gòu)。record與類(lèi)不同,區(qū)別在于record類(lèi)型使用基于值的相等性,下文小編將介紹更多詳細(xì)資料,需要的朋友可以參考一下2022-03-03
asp.net 數(shù)據(jù)庫(kù)連接類(lèi)代碼(SQL)
asp.net數(shù)據(jù)庫(kù)連接類(lèi)(SQL) 代碼,需要的朋友可以參考下。2010-03-03
為T(mén)extBox裝飾水印與(blur和focus)事件應(yīng)用
為了界面的美觀,有些時(shí)候可能需要為文本框(TextBox)裝飾個(gè)水??;它有兩種狀態(tài),一是blur和focus。因此,我們可以在Javascript寫(xiě)兩個(gè)事件,感興趣的朋友可以了解下2013-01-01

