太簡單了,但時間長了,記不得,浪費我半小時找資料,深刻體會好記性不如爛筆頭!!今天把passport文擋貼上,防止以後忘記!!記住,網上找到自己需要的資料也要耗時間的!!!!!!
<?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(),
'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);
}
//=========================================================================
//===========================拷貝結束======================================
?>