將IP地址轉(zhuǎn)換為整型數(shù)字的PHP方法、Asp方法和MsSQL方法、MySQL方法
首先我們要先了解一下IP地址轉(zhuǎn)換為整型(嚴(yán)格來(lái)說(shuō)應(yīng)該說(shuō)是長(zhǎng)整型)的原理~
【轉(zhuǎn)換原理】:假設(shè)IP為:w.x.y.z,則IP地址轉(zhuǎn)為整型數(shù)字的計(jì)算公式為:intIP = 256*256*256*w + 256*256*x + 256*y + z
【PHP的互轉(zhuǎn)】:PHP的轉(zhuǎn)換方式比較簡(jiǎn)單,它內(nèi)置了兩個(gè)函數(shù)
int ip2long ( string $ip_address )和 string long2ip ( string $proper_address )
可以直接調(diào)用使用~
【Asp的互轉(zhuǎn)】:自定義函數(shù)如下,
'.-----------------------------------------------------------.
'| describtion: 將IP轉(zhuǎn)換為int型數(shù)字 |
'| Authors: abandonship(http://jb51.net) |
'~-----------------------------------------------------------~
Function IP2Num(ByVal strIP)
Dim nIP
Dim nIndex
Dim arrIP
arrIP = Split(strIP, ".", 4)
For nIndex = 0 To 3
If Not nIndex = 3 Then
arrIP(nIndex) = arrIP(nIndex) * (256 ^ (3 - nIndex))
End If
nIP = nIP + arrIP(nIndex)
Next
IP2Num = nIP
End Function
'.-----------------------------------------------------------.
'| describtion: 將int型數(shù)字轉(zhuǎn)換為IP |
'| Authors: abandonship(http://jb51.net) |
'~-----------------------------------------------------------~
Function Num2IP(ByVal nIP)
Dim strIP
Dim nTemp
Dim nIndex
For nIndex = 3 To 0 Step -1
nTemp = Int(nIP / (256 ^ nIndex))
strIP = strIP & nTemp & "."
nIP = nIP - (nTemp * (256 ^ nIndex))
Next
strIP = Left(strIP, Len(strIP) - 1)
Num2IP = strIP
End Function
【MsSQL的互轉(zhuǎn)】:自定義函數(shù)如下,
/***************************************************************
* 將IP轉(zhuǎn)換為int型數(shù)字 |
* Code CreateBy abandonship(http://jb51.net) |
**************************************************************/
CREATE FUNCTION [dbo].[ipToInt](
@strIp varchar(15)
)RETURNS bigint
AS
BEGIN
declare @nIp bigint
set @nIp = 0
select
@nIp = @nIp + LEFT( @strIp, charindex('.',@strIp+'.')-1)*Id
from(
select Id = cast(1*256*256*256 as bigint)
union all select 1*256*256
union all select 1*256
union all select 1
) as T
return (@nIp)
END
/***************************************************************
* 將int型數(shù)字轉(zhuǎn)換為IP |
* Code CreateBy abandonship(http://jb51.net) |
**************************************************************/
CREATE FUNCTION [dbo].[intToIP](
@nIp bigint
)RETURNS varchar(15)
As
BEGIN
declare @strIp varchar(15)
set @strIp = ''
select
@strIp = @strIp +'.'+ cast(@nIp/ID as varchar), @nIp = @nIp%ID
from(
select ID = cast(1*256*256*256 as bigint)
union all select 1*256*256
union all select 1*256
union all select 1
) as T
return(stuff(@strIp,1,1,''))
END
【MySQL的互轉(zhuǎn)】:相對(duì)于MsSQL來(lái)說(shuō)MySQL的轉(zhuǎn)換方式比較簡(jiǎn)單,它和PHP一樣也內(nèi)置了兩個(gè)函數(shù)
IP轉(zhuǎn)為整型: select INET_ATON (IP地址) 和 整型轉(zhuǎn)為IP: select INET_NTOA ( IP的整型數(shù)值 )
可以直接調(diào)用使用~
- 易語(yǔ)言將指定的主機(jī)名與IP地址轉(zhuǎn)換功能
- PHP實(shí)現(xiàn)將優(yōu)酷土豆騰訊視頻html地址轉(zhuǎn)換成flash swf地址的方法
- 兩端口路由器地址轉(zhuǎn)換的例子
- Cisco 路由器動(dòng)態(tài)和靜態(tài)地址轉(zhuǎn)換
- FormatRemoteUrl函數(shù)之a(chǎn)sp實(shí)現(xiàn)格式化成當(dāng)前網(wǎng)站完整的URL-將相對(duì)地址轉(zhuǎn)換為絕對(duì)地址的代碼
- 如何把URL和郵件地址轉(zhuǎn)換為超級(jí)鏈接?
- 使用網(wǎng)絡(luò)地址轉(zhuǎn)換實(shí)現(xiàn)多服務(wù)器負(fù)載均衡
- NAT網(wǎng)絡(luò)地址轉(zhuǎn)換詳情
相關(guān)文章
PHP根據(jù)樹(shù)的前序遍歷和中序遍歷構(gòu)造樹(shù)并輸出后序遍歷的方法
這篇文章主要介紹了PHP根據(jù)樹(shù)的前序遍歷和中序遍歷構(gòu)造樹(shù)并輸出后序遍歷的方法,涉及php數(shù)據(jù)結(jié)構(gòu)與算法中關(guān)于數(shù)的遍歷相關(guān)操作技巧,需要的朋友可以參考下2017-11-11深入理解ob_flush和flush的區(qū)別(ob_flush()與flush()使用方法)
ob_flush()和flush()這兩個(gè)函數(shù)一般要一起使用,順序是先ob_flush(),然后flush(),它們的作用是刷新緩沖區(qū)2013-02-02php中對(duì)xml讀取的相關(guān)函數(shù)的介紹一
php中對(duì)xml讀取的相關(guān)函數(shù)的介紹整理如下2008-06-06