将IP地址转换为整型数字的PHP方法、Asp方法和MsSQL方法、MySQL方法
首先我们要先了解一下IP地址转换为整型(严格来说应该说是长整型)的原理~
【转换原理】:假设IP为:w.x.y.z,则IP地址转为整型数字的计算公式为:intIP = 256*256*256*w + 256*256*x + 256*y + z
【PHP的互转】:PHP的转换方式比较简单,它内置了两个函数
int ip2long ( string $ip_address )和 string long2ip ( string $proper_address )
可以直接调用使用~
【Asp的互转】:自定义函数如下,
'.-----------------------------------------------------------.
'| describtion: 将IP转换为int型数字 |
'| 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型数字转换为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的互转】:自定义函数如下,
/***************************************************************
* 将IP转换为int型数字 |
* 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型数字转换为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的互转】:相对于MsSQL来说MySQL的转换方式比较简单,它和PHP一样也内置了两个函数
IP转为整型: select INET_ATON (IP地址) 和 整型转为IP: select INET_NTOA ( IP的整型数值 )
可以直接调用使用~
您可能感兴趣的文章:
- 将IP地址转换为整型数字的PHP方法、Asp方法和MsSQL方法、MySQL方法
- IP地址转换为整型数字的PHP方法、Asp方法和MsSQL方法、MySQL方法
- PHP把MSSQL数据导入到MYSQL的方法
- PHP把MSSQL数据导入到MYSQL的方法
- ASP链接数据库的几种方法(ACCESS,MSSQL,MySql)
- [ASP/PHP/JSP]MySQL5.0中文问题及JDBC数据库连接和JSP汉字编码问题解决方法总结
- PHP+MySQL修改记录的方法
- php调用MSSQL存储过程的方法
- linux Nginx+Php+Mysql+mssql动静页面分离
- ExtJS与PHP、MySQL实现存储的方法
- 【PHP】【Mysql】【thinkphp】,Thinkphp save()方法执行update操作的返回值问题
- linux系统下将php和mysql命令加入到环境变量中的方法
- php通过排列组合实现1到9数字相加都等于20的方法
- phpMyAdmin“无法载入mysql扩展,请检查PHP配置”的解决方法
- 在ORACLE、MSSQL、MYSQL中树结构表递归查询的实现方法
- 升华提供终身免费ASP+access PHP+mysql虚拟主机
- PHP mysql_result()函数使用方法
- php获取mysql版本的几种方法小结
- asp如何取得新插入记录的ID(ACCESS库和MSSQL方法不同) .
- php excel导出数字字符串变成科学技术法导致精度变化的解决方法