將IP地址轉(zhuǎn)換為整型數(shù)字的PHP方法、Asp方法和MsSQL方法、MySQL方法_PHP教程
推薦:php調(diào)用MsSQL存儲(chǔ)過程使用內(nèi)置RETVAL獲取過程中的return值本篇文章是對(duì)php調(diào)用MsSQL存儲(chǔ)過程使用內(nèi)置RETVAL獲取過程中的return值的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下 【PHP代碼】 復(fù)制代碼 代碼如下: $stmt = mssql_init('P__Global_Test', $conn) or die(initialize stored procedure failure); mssql_bind($stmt, R
首先我們要先了解一下IP地址轉(zhuǎn)換為整型(嚴(yán)格來說應(yīng)該說是長(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來說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)用使用~
分享:解析php5配置使用pdo1. 檢查php擴(kuò)展庫(kù)中是否存在php_pdo.dll(當(dāng)調(diào)用MsSQL同時(shí)還需要php_pdo_mssql.dll;當(dāng)調(diào)用MySQL同時(shí)還需要php_pdo_mysql.dll). 2. 打開php.ini配置文件,加入: 指定擴(kuò)展庫(kù) extension_dir=C:\Program Files (x86)\PHP\ext [PHP_PDO] extension=php_pdo.dll [PHP_PDO_M
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國(guó)語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
- 相關(guān)鏈接:
復(fù)制本頁鏈接| 搜索將IP地址轉(zhuǎn)換為整型數(shù)字的PHP方法、Asp方法和MsSQL方法、MySQL方法
- 教程說明:
PHP教程-將IP地址轉(zhuǎn)換為整型數(shù)字的PHP方法、Asp方法和MsSQL方法、MySQL方法
。