<script type="text/javascript"><!-- google_ad_client = "pub-0241434510974184"; /* auto-http.cn 右边 ,468x60 */ google_ad_slot = "0902256228"; google_ad_width = 468; google_ad_height = 60; // --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
现在用Discuz! 6了.网上找了几个现成的接口都不能用,后来看见csdn论坛上面几个人说直接看文档.
文档很详细,照着文档写了个简单
需要配置的地方不多.
1.用户名,密码(如果没有用户就算注册进去了.)
'username' => 'username',
'password' => md5('000000'),
'email' => 'linjin@ine.net.cn',
2.接口操作方式(登录,注册,退出)
$action= 'login'; // 登录,注册
//$action= 'logout'; // 退出
3.Discuz!的URL.
JsGoto("http://127.0.0.1/bbs/api/passport.php".
"?action=$action".
"&auth=".rawurlencode($auth).
"&forward=".rawurlencode($forward).
"&verify=$verify");
4.其它细节Discuz!文档
Discuz! Passport 接口技术文档
http://www.discuz.net/usersguide/advanced_passport.htm#title
<?
php
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
///
/*
*
*Passport加密函数
*
*@paramstring等待加密的原字串
*@paramstring私有密匙(用于解密和加密)
*
*@returnstring原字串经过私有密匙加密后的结果
*/
function
passport_encrypt(
$txt
,
$key
){
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
使用随机数发生器产生0~32000的值并MD5()
srand
((
double
)
microtime
()
*
1000000
);
$encrypt_key
=
md5
(
rand
(
0
,
32000
));
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
变量初始化
$ctr
=
0
;
$tmp
=
''
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
for循环,$i为从0开始,到小于$txt字串长度的整数
for
(
$i
=
0
;
$i
<
strlen
(
$txt
);
$i
++
){
//
如果$ctr=$encrypt_key的长度,则$ctr清零
$ctr
=
$ctr
==
strlen
(
$encrypt_key
)
?
0
:
$ctr
;
//
$tmp字串在末尾增加两位,其第一位内容为$encrypt_key的第$ctr位,
//第二位内容为$txt的第$i位与$encrypt_key的$ctr位取异或。然后$ctr=$ctr+1
$tmp
.=
$encrypt_key
[
$ctr
]
.
(
$txt
[
$i
]
^
$encrypt_key
[
$ctr
++
]);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
返回结果,结果为passport_key()函数返回值的base64编码结果
return
base64_encode
(passport_key(
$tmp
,
$key
));
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
/*
*
*Passport解密函数
*
*@paramstring加密后的字串
*@paramstring私有密匙(用于解密和加密)
*
*@returnstring字串经过私有密匙解密后的结果
*/
function
passport_decrypt(
$txt
,
$key
){
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
$txt的结果为加密后的字串经过base64解码,然后与私有密匙一起,
//经过passport_key()函数处理后的返回值
$txt
=
passport_key(
base64_decode
(
$txt
)
,
$key
);
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
变量初始化
$tmp
=
''
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
for循环,$i为从0开始,到小于$txt字串长度的整数
for
(
$i
=
0
;
$i
<
strlen
(
$txt
);
$i
++
){
//
$tmp字串在末尾增加一位,其内容为$txt的第$i位,
//与$txt的第$i+1位取异或。然后$i=$i+1
$tmp
.=
$txt
[
$i
]
^
$txt
[
++
$i
];
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
返回$tmp的值作为结果
return
$tmp
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
/*
*
*Passport密匙处理函数
*
*@paramstring待加密或待解密的字串
*@paramstring私有密匙(用于解密和加密)
*
*@returnstring处理后的密匙
*/
function
passport_key(
$txt
,
$encrypt_key
){
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
将$encrypt_key赋为$encrypt_key经md5()后的值
$encrypt_key
=
md5
(
$encrypt_key
);
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
变量初始化
$ctr
=
0
;
$tmp
=
''
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
for循环,$i为从0开始,到小于$txt字串长度的整数
for
(
$i
=
0
;
$i
<
strlen
(
$txt
);
$i
++
){
//
如果$ctr=$encrypt_key的长度,则$ctr清零
$ctr
=
$ctr
==
strlen
(
$encrypt_key
)
?
0
:
$ctr
;
//
$tmp字串在末尾增加一位,其内容为$txt的第$i位,
//与$encrypt_key的第$ctr+1位取异或。然后$ctr=$ctr+1
$tmp
.=
$txt
[
$i
]
^
$encrypt_key
[
$ctr
++
];
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
返回$tmp的值作为结果
return
$tmp
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
/*
*
*Passport信息(数组)编码函数
*
*@paramarray待编码的数组
*
*@returnstring数组经编码后的字串
*/
function
passport_encode(
$array
){
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
数组变量初始化
$arrayenc
=
array
();
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
遍历数组$array,其中$key为当前元素的下标,$val为其对应的值
foreach
(
$array
as
$key
=>
$val
){
//
$arrayenc数组增加一个元素,其内容为"$key=经过urlencode()后的$val值"
$arrayenc
[]
=
$key
.
'
=
'
.
urlencode
(
$val
);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
返回以"&"连接的$arrayenc的值(implode),例如$arrayenc=array('aa','bb','cc','dd'),
//则implode('&',$arrayenc)后的结果为”aa&bb&cc&dd"
return
implode
(
'
&
'
,
$arrayenc
);
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
function
JsWrite(
$msg
)
{
echo
"
<scriptlanguage="javascript">
"
;
echo
$msg
;
echo
"
</script>
"
;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
function
JsGoto(
$url
)
{
$msg
=
"
location.href='$url';
"
;
JsWrite(
$msg
);
exit
;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
$passport_key
=
''
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
$member
=
array
(
'
cookietime
'
=>
time
()
,
'
time
'
=>
time
()
,
'
username
'
=>
'username
'
,
'
password
'
=>
md5
(
'0000
00
'
)
,
'
email
'
=>
'
linjin@ine.net.cn
'
,
'
credits
'
=>
''
,
'
regip
'
=>
''
,
'
regdate
'
=>
''
,
'
msn
'
=>
''
);
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
$action
=
'
login
'
;
//
登录,注册
//$action='logout';//退出
$auth
=
passport_encrypt(passport_encode(
$member
)
,
$passport_key
);
$forward
=
'
http://127.0.0.1/bbs/index.php
'
;
$verify
=
md5
(
$action
.
$auth
.
$forward
.
$passport_key
);
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
/*
header("Location:http://127.0.0.1/bbs/api/passport.php".
"?action=$action".
"&auth=".rawurlencode($auth).
"&forward=".rawurlencode($forward).
"&verify=$verify");
*/
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
JsGoto(
"
http://127.0.0.1/bbs/api/passport.php
"
.
"
?action=$action
"
.
"
&auth=
"
.
rawurlencode
(
$auth
)
.
"
&forward=
"
.
rawurlencode
(
$forward
)
.
"
&verify=$verify
"
);
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
?>