Rumah  >  Artikel  >  pembangunan bahagian belakang  >  ThinkPHP实现登录退出功能的方法

ThinkPHP实现登录退出功能的方法

墨辰丷
墨辰丷asal
2018-05-21 10:06:321835semak imbas

这篇文章主要为大家详细介绍了ThinkPHP实现登录退出功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了ThinkPHP实现登录退出功能的具体代码,供大家参考,具体内容如下

<?php 
/**
 * 用户登陆与退出
 * 注册成功后,将页面跳转到login登陆页面
 * 当数据提交到登陆页面后,必须先执行验证码验证通过再执行登陆操作
 */
public function checkyzm($yzm){
 $verify=new \Think\Verify();//实例化TP自带的Verify方法
 if($verify->check($yzm)){//调用check方法
  return true;
 }else{
  echo &#39;验证码错误&#39;;
  exit();
 }
}

public function login(){
 if(!IS_POST){
  $this->display();
 }else{
  //若验证码验证通过
  if($this->checkyzm(I(&#39;post.yzm&#39;))){
   $username=I(&#39;post.username&#39;);
   $user=D( &#39; user &#39; )->where(" username=&#39;$username&#39; ")->find();
   //post过来的password拼接盐md5后密码=数据库的密码,则通过
   if(md5( I( &#39; post.password &#39; ) . $user[ &#39; salt &#39; ])===$user[&#39;password&#39;]{
    cookie(&#39;username&#39; , $userp[ &#39; username &#39; ]);//写入cookie
    $this->redirect(&#39;/&#39;);//跳转到首页
   }else{
    echo &#39;用户名或密码不对&#39;;
   }
  }
 }
}


/**V层
 * 退出登陆
 * 读取cookie $Think.cookie
 */
//若没有cookie数据
您好欢迎观临
<if condition="$Think.cookie.username eq null">
<a href="{:U(&#39;Home/User/login&#39;)}" rel="external nofollow" rel="external nofollow" style="color:#50884b">登陆</a> |
<a href="{:U(&#39;Home/User/reg&#39;)}" rel="external nofollow" style="color:#50884b">免费注册</a> |
<else />
{$Think.cookie.username}|
<<a href="{:U(&#39;Home/User/login&#39;)}" rel="external nofollow" rel="external nofollow" style="color:#50884b">退出</a>
 </if>


 /**
 * 退出方法
 * 1,cookie设为null,删除cookie
 * 2. 跳转到首页
 */
 public function logout(){
  cookie(&#39;username&#39; , null);
  $this->redirect(&#39;/&#39;);
 }

 ?>}

相关推荐:

php ajax 用户登录退出_PHP教程

Yii2实现多域名跨域同步登录退出

Yii2框架自动登录以及登录退出功能的实现方法

Atas ialah kandungan terperinci ThinkPHP实现登录退出功能的方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn