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