欢迎来到代码驿站!

PHP代码

当前位置:首页 > 软件编程 > PHP代码

ThinkPHP实现登录退出功能

时间:2021-07-05 09:24:22|栏目:PHP代码|点击:

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

<?php 
/**
 * 用户登陆与退出
 * 注册成功后,将页面跳转到login登陆页面
 * 当数据提交到登陆页面后,必须先执行验证码验证通过再执行登陆操作
 */
public function checkyzm($yzm){
 $verify=new \Think\Verify();//实例化TP自带的Verify方法
 if($verify->check($yzm)){//调用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后密码=数据库的密码,则通过
   if(md5( I( ' post.password ' ) . $user[ ' salt ' ])===$user['password']{
    cookie('username' , $userp[ ' username ' ]);//写入cookie
    $this->redirect('/');//跳转到首页
   }else{
    echo '用户名或密码不对';
   }
  }
 }
}


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


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

 ?>}

上一篇:php使用gettimeofday函数返回当前时间并存放在关联数组里

栏    目:PHP代码

下一篇:PHP的全局错误处理详解

本文标题:ThinkPHP实现登录退出功能

本文地址:http://www.codeinn.net/misctech/153130.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有