thinkphp框架下的登录、注册、找密码

thinkphp框架下使用ajax表单提交的登录、注册、找密码。注册后的用户需后台审核。user表的字段为id、
num、password、name、email、addtime、status
<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
    //处理登录
    public function signin(){  
    	if(IS_GET){  
    		$this->display();
    	} 
    	if(IS_POST){
            /* 调用登录接口登录 */ 
            $User = M('user') ;   
            //I方法接收页面传递来的值
            $num = I('num') ; 
            $password = I('password') ;
            //查找user表中num等于$num的值
            $datanum = $User->where(array('num'=>$num))->find();
            //判断$datanum的值
            if ($datanum){
            	if (md5($password) === $datanum['password']) { 
            		if ($datanum['status'] == 0) {
            			$this->error('用户处于未审核状态,请联系管理员');
            		}elseif($datanum['status'] == 2){
                        $this->error('用户处于禁用状态,请联系管理员');
                    }else{
            			$this->autoLogin($datanum) ; //调用私有方法自动登录.  
     					$uid = $datanum['id'];
     					if($_SESSION['user_auth']['uid'] && $_SESSION['user_auth']['role'] == 'user'){
		                    $this->success('登录成功!', U('Index/index'));
		                }else{
		                    $this->error('存储错误.');
		                }
            		}
            	}else{
 					$this->error('密码填写不正确,请重新填写'); 
 					exit();
            	}
            }else{
            	$this->error('用户不存在,请注册',U('signup'));
            }
    	}
    }

    public function autoLogin($user){   
        /* 记录登录SESSION */
        $auth = array(
            'uid'             => $user['id'],
            'num'        => $user['num'], 
            'role'			  => 'user' ,  //记录用户类型 
        );
        session('user_auth', $auth);
        session('user_auth_sign', data_auth_sign($auth)); 
    }

    /*
    * 用户注册 
    */
    public function signup(){
        if(is_user_login()){
            $this->redirect('Index/index');
        }
        if(IS_GET){
            //注册页面
            $this->display();
        }
        if(IS_POST){
            //判断用户 
            $data['num'] = I('num') ; 
            $User = M('user') ;
            $datanum = $User->where($data)->find();
            if ($datanum){
            	$this->success('您已经注册过,请直接登录',U('signin'));
            }else{
	            $data['password'] = md5(I('password')); 
	            $data['name']   = I('name');
	            $data['email']   = I('email');
	            $data['addtime'] = time();
	            $uid = $User->add($data);
	            if($uid) 
	            	$this->success('注册成功',U('signin')) ; 
	            else     
	            	$this->error('注册失败') ; 
            }
        }
    }

    public function logout(){
        if(is_user_login()){
            $User = M('user') ;
            session('user_auth', null);
            session('user_auth_sign', null);
            session('[destroy]');
            $this->success('登出成功!', U('signin'));
        } else {
            $this->redirect('signin');
        }
    }

    //忘记密码
    public function wjpas(){  
        if(IS_GET){  
            $this->display();
        } 
        if(IS_POST){
            $User = M('user') ;
            $num = I('num') ;
            $data['password'] = md5(I('password')) ; 
            $email = I('email') ;
            $datanum = $User->where(array('num'=>$num))->find();
            if ($datanum){
                if ($email === $datanum['email']) {
                    $User->where(array('num'=>$num))->save($data); // 根据条件更新记录
                    $this->success('密码修改成功',U('signin')) ; 
                }else{
                    $this->error('邮箱填写不正确,请重新填写'); 
                    exit();
                }
            }else{
                $this->error('用户不存在,请注册',U('signup'));
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Bootstrap Admin</title>
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/stylesheets/theme.css">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.css">
    <script src="__PUBLIC__/jquery-1.8.1.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="__PUBLIC__/layer/layer.min.js"></script>
    <script src="__PUBLIC__/bootstrap/js/bootstrap.js"></script>
    <script type="text/javascript" src='__PUBLIC__/layer/extend/layer.ext.js'></script>

    <!-- Demo page code -->
    <style type="text/css">
        #line-chart {
            height:300px;
            width:800px;
            margin: 0px auto;
            margin-top: 1em;
        }
        .brand { font-family: georgia, serif; }
        .brand .first {
            color: #ccc;
            font-style: italic;
        }
        .brand .second {
            color: #fff;
            font-weight: bold;
        }
    </style>

    <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="__PUBLIC__/assets/ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="__PUBLIC__/assets/ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="__PUBLIC__/assets/ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="__PUBLIC__/assets/ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="__PUBLIC__/assets/ico/apple-touch-icon-57-precomposed.png">
  </head>

  <!--[if lt IE 7 ]> <body class="ie ie6"> <![endif]-->
  <!--[if IE 7 ]> <body class="ie ie7"> <![endif]-->
  <!--[if IE 8 ]> <body class="ie ie8"> <![endif]-->
  <!--[if IE 9 ]> <body class="ie ie9"> <![endif]-->
  <!--[if (gt IE 9)|!(IE)]><!--> 
  <body> 
  <!--<![endif]-->    
    <div class="navbar">
        <div class="navbar-inner">
            <div class="container-fluid">
                <ul class="nav pull-right">
                    
                </ul>
                <a class="brand" href=""><span class="first">CSV</span> <span class="second">&nbsp;drawing &nbsp;tool</span></a>
            </div>
        </div>
    </div>
    

    <div class="container-fluid">
        
        <div class="row-fluid">
    <div class="dialog span4">
        <div class="block">
            <div class="block-heading">登录</div>
            <div class="block-body">
                <form name="signin" id="form1" method="post" action="{:U('Login/signin')}">
                    <label>工号</label>
                    <input type="text" class="span12" name="num" value="" id="num">
                    <label>密码</label>
                    <input type="password" class="span12" name="password" value="" id="psw">
                    <button type="submit" target-form="form1" class="am-btn am-btn-primary am-btn-block ajax-post" style="display:none;">提交</button>
                   <button type="button" onclick="sub(this.form,this)" class="btn btn-primary pull-right" >登录</button>
                    <label class="remember-me"><input type="checkbox"> 记住我</label>
                    <div class="clearfix"></div>
                </form>
            </div>
        </div>
        <p class="pull-right" style=""><a href="{:U('Login/signup')}" target="_blank">注册</a></p>
        
        <p><a href="{:U('Login/wjpas')}" target="_blank">忘记密码?</a></p>
    </div>
</div>

<script type="text/javascript" src="__PUBLIC__/Js/login.js"></script>
<script type="text/javascript">
function sub(o){
    //表单验证部分
    if($("#num").val() == ""){
        layer.alert('工号不能为空') ;
        $("#num").focus();
        return false ;
    }
    if($("#psw").val() == ""){
        layer.alert('密码不能为空') ;
        $("#psw").focus();
        return false ;
    }
    $(".ajax-post").trigger('click') ; 
}
</script>
    

    

    

    <!-- Le javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    
    
    
    
    
    
    
    
    
    
    
    

  </body>
</html>


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Bootstrap Admin</title>
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/stylesheets/theme.css">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.css">
    <script src="__PUBLIC__/jquery-1.8.1.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="__PUBLIC__/layer/layer.min.js"></script>
    <script src="__PUBLIC__/bootstrap/js/bootstrap.js"></script>
    <script type="text/javascript" src='__PUBLIC__/layer/extend/layer.ext.js'></script>

    <!-- Demo page code -->
    
    <style type="text/css">
        #line-chart {
            height:300px;
            width:800px;
            margin: 0px auto;
            margin-top: 1em;
        }
        .brand { font-family: georgia, serif; }
        .brand .first {
            color: #ccc;
            font-style: italic;
        }
        .brand .second {
            color: #fff;
            font-weight: bold;
        }
        .block-heading font{
            font-weight: lighter;
            margin-left: 10px;
            font-size: 13px;
            color: #0088cc;
            font-family: serif;
        }
    </style>

    <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="__PUBLIC__/assets/ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="__PUBLIC__/assets/ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="__PUBLIC__/assets/ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="__PUBLIC__/assets/ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="__PUBLIC__/assets/ico/apple-touch-icon-57-precomposed.png">
  </head>
  <!--[if lt IE 7 ]> <body class="ie ie6"> <![endif]-->
  <!--[if IE 7 ]> <body class="ie ie7"> <![endif]-->
  <!--[if IE 8 ]> <body class="ie ie8"> <![endif]-->
  <!--[if IE 9 ]> <body class="ie ie9"> <![endif]-->
  <!--[if (gt IE 9)|!(IE)]><!--> 
  <body> 
  <!--<![endif]-->    
    <div class="navbar">
        <div class="navbar-inner">
            <div class="container-fluid">
                <ul class="nav pull-right">
                    
                </ul>
                <a class="brand" href=""><span class="first">CSV</span> <span class="second">&nbsp;drawing &nbsp;tool</span></a>
            </div>
        </div>
    </div>
    

    <div class="container-fluid">
        
        <div class="row-fluid">
    <div class="span4 offset4 dialog">
        <div class="block">
            <div class="block-heading">注册<font>带*的为必填项</font></div>
            <div class="block-body">
                <form name="signup" id="form1" method="post" action="{:U('Login/signup')}">
                    <label>*工号</label>
                    <input type="text" class="span12" name="num" value="" id="num">
                    <label>*姓名</label>
                    <input type="text" class="span12" name='name' value="" id="name">
                    <label>*邮箱</label>
                    <input type="text" class="span12" name="email" value="" id="email">
                    <label>*密码</label>
                    <input type="password" class="span12" name="password" value="" id="psw">
                    <button type="submit" target-form="form1" class="am-btn am-btn-primary am-btn-block ajax-post" style="display:none;">提交</button>
                   <button type="button" onclick="sub(this.form,this)"class="btn btn-primary pull-right" >注册</button>
                    <!-- <a href="" class="btn btn-primary pull-right">注册</a> -->
                    <label class="remember-me"><input type="checkbox"> 我同意<a href="terms-and-conditions.html">该网站使用协议</a></label>
                    <div class="clearfix"></div>
                </form>
            </div>
        </div>
        <p><a href="{:U('Login/signin')}">已注册过,立即登录</a></p>
    </div>
</div>
<script type="text/javascript" src="__PUBLIC__/Js/login.js"></script>
<script type="text/javascript">
function sub(o){
    if($("#num").val() == ""){
        layer.alert('工号不能为空') ;
        $("#num").focus();
        return false ;
    }
    if($("#name").val() == ""){
        layer.alert('姓名不能为空') ;
        $("#name").focus();
        return false ;
    }
    if ($("#email").val() == "") {
        layer.alert('邮箱不能为空') ;
        $("#email").focus();
        return false ;
    }
    if (!$("#email").val().match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)) { 
        layer.alert("邮箱格式不正确");  
        $("#email").focus(); 
        return false; 
    } 

    if($("#psw").val() == ""){
        layer.alert('密码不能为空') ;
        $("#psw").focus();
        return false ;
    }
    //表单验证部分
    $(".ajax-post").trigger('click') ; 
}
</script>
  </body>
</html>


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Bootstrap Admin</title>
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/stylesheets/theme.css">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.css">
    <script src="__PUBLIC__/jquery-1.8.1.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="__PUBLIC__/layer/layer.min.js"></script>
    <script src="__PUBLIC__/bootstrap/js/bootstrap.js"></script>
    <script type="text/javascript" src='__PUBLIC__/layer/extend/layer.ext.js'></script>

    <!-- Demo page code -->
    <style type="text/css">
        #line-chart {
            height:300px;
            width:800px;
            margin: 0px auto;
            margin-top: 1em;
        }
        .brand { font-family: georgia, serif; }
        .brand .first {
            color: #ccc;
            font-style: italic;
        }
        .brand .second {
            color: #fff;
            font-weight: bold;
        }
        .block-heading font{
            font-weight: lighter;
            font-size: 13px;
            color: #0088cc;
            font-family: serif;
        }
    </style>

    <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="__PUBLIC__/assets/ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="__PUBLIC__/assets/ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="__PUBLIC__/assets/ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="__PUBLIC__/assets/ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="__PUBLIC__/assets/ico/apple-touch-icon-57-precomposed.png">
  </head>

  <!--[if lt IE 7 ]> <body class="ie ie6"> <![endif]-->
  <!--[if IE 7 ]> <body class="ie ie7"> <![endif]-->
  <!--[if IE 8 ]> <body class="ie ie8"> <![endif]-->
  <!--[if IE 9 ]> <body class="ie ie9"> <![endif]-->
  <!--[if (gt IE 9)|!(IE)]><!--> 
  <body> 
  <!--<![endif]-->    
    <div class="navbar">
        <div class="navbar-inner">
            <div class="container-fluid">
                <ul class="nav pull-right">
                    
                </ul>
                <a class="brand" href=""><span class="first">CSV</span> <span class="second">&nbsp;drawing &nbsp;tool</span></a>
            </div>
        </div>
    </div>
    

    <div class="container-fluid">
        
        <div class="row-fluid">
    <div class="dialog span4">
        <div class="block">
            <div class="block-heading">忘记密码<font>(输入注册时填写的工号和邮箱)</font></div>
            <div class="block-body">
                <form name="signin" id="form1" method="post" action="{:U('Login/wjpas')}">
                    <label>工号</label>
                    <input type="text" class="span12" name="num" value="" id="num">
                    <label>邮箱</label>
                    <input type="text" class="span12" name="email" value="" id="email">
                    <label>新密码</label>
                    <input type="password" class="span12" name="password" value="" id="psw">
                    <button type="submit" target-form="form1" class="am-btn am-btn-primary am-btn-block ajax-post" style="display:none;">提交</button>
                   <button type="button" onclick="sub(this.form,this)" class="btn btn-primary pull-right" >提交</button>
                    <div class="clearfix"></div>
                </form>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="__PUBLIC__/Js/login.js"></script>
<script type="text/javascript">
function sub(o){
    //表单验证部分
    if($("#num").val() == ""){
        layer.alert('工号不能为空') ;
        $("#num").focus();
        return false ;
    }
    if($("#email").val() == ""){
        layer.alert('邮箱不能为空') ;
        $("#email").focus();
        return false ;
    }
    if($("#psw").val() == ""){
        layer.alert('新密码不能为空') ;
        $("#psw").focus();
        return false ;
    }
    $(".ajax-post").trigger('click') ; 
}
</script>
  </body>
</html>
编程技巧