《PHP編程:ThinkPHP實現(xiàn)登錄退出功能》要點:
本文介紹了PHP編程:ThinkPHP實現(xiàn)登錄退出功能,希望對您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:thinkphp教程
PHP應(yīng)用本文實例為大家分享了ThinkPHP實現(xiàn)登錄退出功能的具體代碼,供大家參考,具體內(nèi)容如下
PHP應(yīng)用
<?php
/**
* 用戶登陸與退出
* 注冊成功后,將頁面跳轉(zhuǎn)到login登陸頁面
* 當(dāng)數(shù)據(jù)提交到登陸頁面后,必須先執(zhí)行驗證碼驗證通過再執(zhí)行登陸操作
*/
public function checkyzm($yzm){
$verify=new \Think\Verify();//實例化TP自帶的Verify方法
if($verify->check($yzm)){//調(diào)用check方法
return true;
}else{
echo '驗證碼錯誤';
exit();
}
}
public function login(){
if(!IS_POST){
$this->display();
}else{
//若驗證碼驗證通過
if($this->checkyzm(I('post.yzm'))){
$username=I('post.username');
$user=D( ' user ' )->where(" username='$username' ")->find();
//post過來的password拼接鹽md5后密碼=數(shù)據(jù)庫的密碼,則通過
if(md5( I( ' post.password ' ) . $user[ ' salt ' ])===$user['password']{
cookie('username' , $userp[ ' username ' ]);//寫入cookie
$this->redirect('/');//跳轉(zhuǎn)到首頁
}else{
echo '用戶名或密碼不對';
}
}
}
}
/**V層
* 退出登陸
* 讀取cookie $Think.cookie
*/
//若沒有cookie數(shù)據(jù)
您好歡迎觀臨
<if condition="$Think.cookie.username eq null">
<a href="{:U('Home/User/login')}" rel="external nofollow" rel="external nofollow" style="color:#50884b">登陸</a> |
<a href="{:U('Home/User/reg')}" rel="external nofollow" style="color:#50884b">免費(fèi)注冊</a> |
<else />
{$Think.cookie.username}|
<<a href="{:U('Home/User/login')}" rel="external nofollow" rel="external nofollow" style="color:#50884b">退出</a>
</if>
/**
* 退出方法
* 1,cookie設(shè)為null,刪除cookie
* 2. 跳轉(zhuǎn)到首頁
*/
public function logout(){
cookie('username' , null);
$this->redirect('/');
}
?>}
PHP應(yīng)用以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持維易PHP.
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/555.html