thinkphp实现自动登录

网页上经常有一些自动登录的checkbox,勾选后,下次进入该网站,无需登录,即可执行一些需要登录才能执行的操作。上班无事,用thinkphp做了下

1 下面是一个很普通的form表单,有一个checkbox 用来标志是否选择了自动登录。

tpl/Login/index.html

<div class='demo'>
<form action="{:U(dologin)}" method="post">
用户名:<input type="text" name="username"/><br/>
密 码:<input type="password" name="password"/><br/>
<input type="checkbox" name='auto'/>自动登录<br/>
<input type="submit" value='提交'/>
</form>
</div>

2 下面是提交处理方法 Lib/Action/loginAction.class.php

  //处理登陆表单提交
    public function dologin() {
        if (!$this->isPost()) {
            halt('页面不存在');
        }
        $m = M('user');
        //dump($m);
        $username = $this->_post('username');
        $password = $this->_post('password');
        $where = array('username' => $username);
        //判断用户名是否存在
        if ($m->where($where)->getField('id')) {

            $where = array('username' => $username, 'password' => $password);
            //判断用户名密码是否一致
            if ($id = $m->where($where)->getField($id)) {
                $_SESSION['uid'] = $id;
                //如果勾选了自动登录,则将用户名和ip写入cookie中
                if (isset($_POST['auto'])) {
                    $ip=  get_client_ip();
                    $value=$username."|".$ip;
                    
                    $value=encryption($value);
                    setcookie('auto',$value,C('AUTO_LOGIN_TIME'),'/');
                }
                header("content-type:text/html;charset=utf-8");
                $this->redirect('/Index/index', array(), 3, '登陆成功,正在跳转');

                
            }
        } else {
            $this->error('用户名不存在');
        }
    }

可以看到代码整体基本都是用户名密码的验证,其中isset($_POST['auto'])是勾选自动登录后,写入cookie。其中encryption()函数是一个加密函数

3写一个action基类,CommonAction常用的Action都去继承这个类

class CommonAction extends Action {

    Public function _initialize() {
        //如果$_COOKIE['auto']存在,并且用户不在登录状态
        if(isset($_COOKIE['auto']) && !$_SESSION['uid']){
            $value= explode('|',encryption($_COOKIE['auto'],1));
            //查看ip是否一致
            if($value[1]=  get_client_ip()){
                $m=M('user');
                $where=array('username'=>$value[0]);
                //检查用户名
                if($id=$m->where($where)->getField('id')){
                    $_SESSION['uid']=$id;
                }
            }
        }
        if(!isset($_SESSION['uid'])){
           
            redirect(U('login/index'));
        }
    }

}
if(isset($_COOKIE['auto']) && !$_SESSION['uid']){}这个判断用来判断用户的cookie中有auto值,并且没有登录时,假如验证成功,则进一步验证ip与上次ip是否一致,验证用户名是否存在于数据库中,如果验证通过,则写入session,由此实现自动登录。其中encryption($_COOKIE['auto'],1)
是一个解密函数,将加密后的用户名和密码解密出来进行验证。

ps:加密解密函数如下,可以写在common/common.php作为函数进行使用
/**
 * 
 * @param string $value 要加密的值
 * @param int $type     0加密  1解密
 */
function encryption($value,$type=0){
     $key=md5(C('ENCRYPTION_KEY'));
    if($type==0){
       
        $value=$key^$value;
        $value=  str_replace('=','',base64_encode($value));
        return $value;
    }else if($type==1){
        $value=  base64_decode($value);
        return $value^$key;
    }
}
原文地址:https://www.cnblogs.com/kongxs/p/3423181.html