<?php
//该文档保存为login.php
//首先将接口技术文档里的加密解密函数拷贝
//为了不让代码太乱,我拷贝到文档的结尾处
//假设自己的用户数据库表里用户名字段为UserName, 密码字段为Pwd, Email字段为 Email
//注册页实现方法差不多,可自行实现,疑问加我QQ:2666556
$act=(isset($_GET['act']))?$_GET['act']:"login";
if(function_exists($act)) $act();else login();
function login()
{
$ErrMsg=UserCheck();
if($ErrMsg!="")echo $ErrMsg;
//后面加上显示你的登陆表单的代码 如
?>
<form action=login.php?act=login method=post>
用户名:<input name=username>
密码:<input name=password>
<input name=submit type=submit value=登陆></form>
<?php
}//end function
function logout()//登出
{
$passportkey="1234567890";//这里换成你论坛通行证设置的passportkey
$auth=$_COOKIE['auth'];
setcookie("auth", "",time() - 3600);
$forward=$_GET['forward'];
if($forward=="")$forward="../../index.php";//这里换成你的主页绝对地址或相对地址
$verify = md5('logout'.$auth.$forward.$passportkey);
$auth=rawurlencode($auth);
$forward=rawurlencode($forward);
header("Location: bbs/api/passport.php?action=logout&auth=$auth&forward=$forward&verify=$verify");
}
function UserCheck()
{
$passportkey="1234567890";//这里换成你论坛通行证设置的passportkey
//===========验证输入=====================
if(!isset($_POST['submit'])) return; // login表单的按钮需要与此同名
$usnm=$_POST['username'];//username换成你登陆表单里的用户名域
$pwd=$_POST['password'];//password换成你登陆表单里的密码域
if($usnm=="") return "请输入用户名!";
if($pwd=="") return "请输入密码!";
//=========数据库处理==========================
$db=mysql_connect("localhost", "root", "");
mysql_select_db("your_db_name");
$sql="Select * from `user` where UserName='".$usnm."' Limit 1";
$rs = mysql_query($sql,$db) ;
$row = mysql_fetch_array($rs);
if(!$row)return "该用户不存在";
if($row["Pwd"]!=md5($pwd))return "密码错误";
mysql_free_result($rs);
//==============header到bbs=====================
$member = array
(
'time' => time(),
&nbs
p; 'username' => $row["UserName"],
'password' => $row["Pwd"],
'email' => $row["Email"]
);
$auth = passport_encrypt(passport_encode($member), $passportkey);
setcookie("auth",$auth,($_POST["Cookie"]? time()+(int)$_POST["Cookie"] :0));
$forward=$_POST['forward'];
if($forward=="")$forward="../../index.php";
$verify = md5('login'.$auth.$forward.$passportkey);
$auth=rawurlencode($auth);
$forward=rawurlencode($forward);
header("Location: bbs/api/passport.php?action=login&auth=$auth&forward=$forward&verify=$verify");
}
//=============================================================
//=============以下为拷贝过来的函数============================
function passport_encrypt($txt, $key) {
srand((double)microtime() * 1000000);
$encrypt_key = md5(rand(0, 32000));
$ctr = 0;
$tmp = '';
for($i = 0; $i < strlen($txt); $i++) {
$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
$tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
}
return base64_encode(passport_key($tmp, $key));
}
function passport_decrypt($txt, $key) {
$txt = passport_key(base64_decode($txt), $key);
$tmp = '';
for ($i = 0; $i < strlen($txt); $i++) {
$tmp .= $txt[$i] ^ $txt[++$i];
}
return $tmp;
}
function passport_key($txt, $encrypt_key) {
$encrypt_key = md5($encrypt_key);
$ctr = 0;
$tmp = '';
for($i = 0; $i < strlen($txt); $i++) {
$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
$tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
}
return $tmp;
}
function passport_encode($array) {
$arrayenc = array();
foreach($array as $key => $val) {
$arrayenc[] = $key.'='.urlencode($val);
}
return implode('&', $arrayenc);
}
//=========================================================================
//===========================拷贝结束======================================
?>