首先我们要先了解一下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://) |
'~-----------------------------------------------------------~
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://) |
'~-----------------------------------------------------------~
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://) |
**************************************************************/
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://) |
**************************************************************/
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的整型数值 )
可以直接调用使用~
setcookie("ICNet[Reg]", "This is reg cookie", time() + 3600);
setcookie("ICNet[Active]", "This is login cookie.", time() + 3600);
print_r($_COOKIE);
/*Result:
Array ( [ICNet] => Array ( [Reg] => This is reg cookie [Active] => This is login cookie ) [PHPSESSID] => nsi2q5upuvqrik6notaohjd7p3 )
*/
读取:
echo $_COOKIE['ICNet']['Reg'];
echo $_COOKIE['ICNet']['Active'];
//或者
$arr_cookie = $_COOKIE['ICNet'];
echo $arr_cookie['Reg'];
echo $arr_cookie['Active'];
销毁:
setcookie('ICNet[Reg]', '', time() - 3600);
setcookie('ICNet[Active]', '', time() - 3600);
$dbh = new PDO('sqlsrv:server=连接地址;Database=数据库名', 用户名, 密码);
try {
$procName = "P_Test_GetMixData";
$stmt = $dbh->prepare("EXEC $procName ?, ?, ?");
$nReturnValue = 0;
$strReturnValue = "";
$strSearchValue = "abandonship";
$stmt->bindParam(1, $nReturnValue, PDO::PARAM_INT | PDO::PARAM_INPUT_OUTPUT, PDO::SQLSRV_PARAM_OUT_DEFAULT_SIZE);
$stmt->bindParam(2, $strReturnValue, PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(3, $strSearchValue , PDO::PARAM_STR);
$stmt->execute();
//获取第一个结果集.
$rowset_1 = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($rowset_1);
echo '<br><br>';
//获取第二个结果集.
$stmt->nextRowset();
$rowset_2 = $stmt->fetch();
print_r($rowset_2);
echo '<br><br>';
$stmt->nextRowset();
// 获取两个输出类型的参数
echo $nReturnValue.'<br><br>';
echo $strReturnValue;
} catch (Exception $e) {
echo $e->getMessage();
}
【SQL PROCEDURE】:
/**
* 用于测试PDO调用MsSQLServer2012存储过程获取复合结果集Demo
* Code CreateBy abandonship 2012.10.11
**/
CREATE PROCEDURE [dbo].[P_Test_GetMixData](
@Message_1 tinyint output,
@Messgae_2 varchar(10) output,
@SearchValue varchar(50)
) As
set nocount on
set @Message_1 = 123
set @Messgae_2 = 'Hi,there!This is abandonship!'
select * from _T1 where col1 like '%'+@SearchValue+'%'
select * from _T2 where col1 like '%'+@SearchValue+'%'
set nocount off
【一些要注意的问题】:当bindParam中存在需要输出类型的参数时,必须包含长度($length)。
【备注】:$length: An optional (integer) length of the data type. You can specify PDO::SQLSRV_PARAM_OUT_DEFAULT_SIZE to indicate the default size when using PDO::PARAM_INT or PDO::PARAM_BOOL in $data_type.