thinkphp 登录注册页面
- 格式:doc
- 大小:41.00 KB
- 文档页数:7
创建数据库:/*Navicat MySQL Data TransferSource Server : 本地连接Source Server Version : 50617Source Host : localhost:3306Source Database : crmTarget Server Type : MYSQLTarget Server Version : 50617File Encoding : 65001Date: 2015-06-29 23:55:28*/SET FOREIGN_KEY_CHECKS=0;-- ------------------------------ Table structure for think_users-- ----------------------------DROP TABLE IF EXISTS `think_users`;CREATE TABLE `think_users` (`userid` mediumint(8) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户id', `companyid` mediumint(8) unsigned NOT NULL COMMENT '公司id',`pid` mediumint(8) NOT NULL COMMENT '父id',`username` char(20) NOT NULL DEFAULT '' COMMENT '用户名',`password` char(32) NOT NULL DEFAULT '' COMMENT '密码',`nickname` char(20) NOT NULL DEFAULT '' COMMENT '昵称',`regdate` int(10) unsigned NOT NULL COMMENT '注册时间',`lastdate` int(10) unsigned NOT NULL COMMENT '最后一次登录时间',`regip` char(15) NOT NULL DEFAULT '' COMMENT '注册ip',`lastip` char(15) NOT NULL DEFAULT '' COMMENT '最后一次登录ip',`loginnum` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '登录次数',`email` char(32) NOT NULL DEFAULT '' COMMENT '邮箱',`mobile` char(11) NOT NULL DEFAULT '' COMMENT '手机号码',`islock` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否锁定',`vip` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否会员',`overduedate` int(10) unsigned NOT NULL COMMENT '账户过期时间',`status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态-用于软删除', PRIMARY KEY (`userid`),UNIQUE KEY `username` (`username`) USING BTREE,KEY `email` (`email`) USING BTREE) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;创建模型(用于自动验证, 自动完成) :namespace Home\Model;use Think\Model;class UsersModel extends Model {/*** 自动验证* self::EXISTS_VALIDATE 或者0 存在字段就验证(默认)* self::MUST_VALIDATE 或者1 必须验证* self::VALUE_VALIDATE或者2 值不为空的时候验证*/protected $_validate = array(array('nickname', 'require', '昵称不能为空!'), //默认情况下用正则进行验证array('username', 'require', '用户名不能为空!'), //默认情况下用正则进行验证array('username', '', '该用户名已被注册!', 0, 'unique', 1), // 在新增的时候验证name 字段是否唯一array('email', '', '该邮箱已被占用', 0, 'unique', 1), // 新增的时候email字段是否唯一array('mobile', '', '该手机号码已被占用', 0, 'unique', 1), // 新增的时候mobile字段是否唯一// 正则验证密码[需包含字母数字以及@*#中的一种,长度为6-22位]array('password', '/^([a-zA-Z0-9@*#]{6,22})$/', '密码格式不正确,请重新输入!', 0),array('repassword', 'password', '确认密码不正确', 0, 'confirm'), // 验证确认密码是否和密码一致array('email', 'email', '邮箱格式不正确'), // 内置正则验证邮箱格式array('mobile', '/^1[34578]\d{9}$/', '手机号码格式不正确', 0), // 正则表达式验证手机号码array('verify', 'verify_check', '验证码错误', 0, 'function'), // 判断验证码是否正确//array('agree', 'is_agree', '请先同意网站安全协议!', 1, 'callback'), // 判断是否勾选网站安全协议array('agree', 'require', '请先同意网站安全协议!', 1), // 判断是否勾选网站安全协议);/*** 自动完成*/protected $_auto = array (array('password', 'md5', 3, 'function') , // 对password字段在新增和编辑的时候使md5函数处理array('regdate', 'time', 1, 'function'), // 对regdate字段在新增的时候写入当前时间戳array('regip', 'get_client_ip', 1, 'function'), // 对regip字段在新增的时候写入当前注册ip地址);/*** 判断是否同意网站安全管理协议* @return boolprotected function is_agree(){// 获取POST数据$agree = I('post.agree', 0, 'intval');// 验证if ($agree) {return true;} else {return false;}}复制代码登录注册:namespace Home\Controller;use Think\Controller;/*** Class LoginController* @package Home\Controller*/class LoginController extends Controller {/*** 用户登录*/public function login(){// 判断提交方式if (IS_POST) {// 实例化Login对象$login = D('login');// 自动验证创建数据集if (!$data = $login->create()) {// 防止输出中文乱码header("Content-type: text/html; charset=utf-8");exit($login->getError());}// 组合查询条件$where = array();$where['username'] = $data['username'];$result = $login->where($where)->field('userid,username,nickname,password,lastdate,lastip')->find();// 验证用户名对比密码if ($result && $result['password'] == $result['password']) {// 存储sessionsession('uid', $result['userid']); // 当前用户idsession('nickname', $result['nickname']); // 当前用户昵称session('username', $result['username']); // 当前用户名session('lastdate', $result['lastdate']); // 上一次登录时间session('lastip', $result['lastip']); // 上一次登录ip// 更新用户登录信息$where['userid'] = session('uid');M('users')->where($where)->setInc('loginnum'); // 登录次数加1M('users')->where($where)->save($data); // 更新登录时间和登录ip$this->success('登录成功,正跳转至系统首页...', U('Index/index'));} else {$this->error('登录失败,用户名或密码不正确!');}} else {$this->display();}}/*** 用户注册*/public function register(){// 判断提交方式做不同处理if (IS_POST) {// 实例化User对象$user = D('users');// 自动验证创建数据集if (!$data = $user->create()) {// 防止输出中文乱码header("Content-type: text/html; charset=utf-8");exit($user->getError());}//插入数据库if ($id = $user->add($data)) {/* 直接注册用户为超级管理员,子用户采用邀请注册的模式,遂设置公司id等于注册用户id,便于管理公司用户*/$user->where("userid = $id")->setField('companyid', $id);$this->success('注册成功', U('Index/index'), 2);} else {$this->error('注册失败');}} else {$this->display();}}/*** 用户注销*/public function logout(){// 清楚所有sessionsession(null);redirect(U('Login/login'), 2, '正在退出登录...');}/*** 验证码*/public function verify(){// 实例化Verify对象$verify = new \Think\Verify();// 配置验证码参数$verify->fontSize = 14; // 验证码字体大小$verify->length = 4; // 验证码位数$verify->imageH = 34; // 验证码高度$verify->useImgBg = true; // 开启验证码背景$verify->useNoise = false; // 关闭验证码干扰杂点$verify->entry();}}复制代码登录模版:<form action="__SELF__" method="post"><div class="form-group has-feedback"><input type="text" name="username" class="form-control" placeholder="用户名" /><span class="glyphicon glyphicon-user form-control-feedback"></span> </div><div class="form-group has-feedback"><input type="password" name="password" class="form-control" placeholder="密码" /><span class="glyphicon glyphicon-lock form-control-feedback"></span> </div><div class="form-group has-feedback"><input type="text" name="verify" class="form-control" placeholder="验证码" style="width:200px;" /><span class="glyphicon glyphicon-qrcode form-control-feedback" style="right:120px;"></span><img class="verify" src="{:U(verify)}" alt="验证码" onClick="this.src=this.src+'?'+Math.random()" /></div><div class="row"><div class="col-xs-8"><div class="checkbox icheck"><label><input type="checkbox" name="remember" value="1"> 记住我</label></div></div><!-- /.col --><div class="col-xs-4"><button type="submit" class="btn btn-primary btn-block btn-flat">登录</button></div><!-- /.col --></div></form>复制代码注册模版:<div class="register-box-body"><p class="login-box-msg">注册一个新用户</p><form action="__SELF__" method="post"><div class="form-group has-feedback"><input type="text" name="nickname" class="form-control" placeholder="昵称" /><span class="glyphicon glyphicon-leaf form-control-feedback"></span> </div><div class="form-group has-feedback"><input type="text" name="username" class="form-control" placeholder="用户名" /><span class="glyphicon glyphicon-user form-control-feedback"></span> </div><div class="form-group has-feedback"><input type="password" name="password" class="form-control" placeholder="密码" /><span class="glyphicon glyphicon-credit-card form-control-feedback"></span></div><div class="form-group has-feedback"><input type="password" name="repassword" class="form-control" placeholder="确认密码" /><span class="glyphicon glyphicon-check form-control-feedback"></span> </div><div class="form-group has-feedback"><input type="email" name="email" class="form-control" placeholder="邮箱" /><span class="glyphicon glyphicon-envelope form-control-feedback"></span> </div><div class="form-group has-feedback"><input type="text" name="mobile" class="form-control" placeholder="手机号码" /><span class="glyphicon glyphicon-phone form-control-feedback"></span> </div><div class="form-group has-feedback"><input type="text" name="verify" class="form-control" placeholder="验证码" style="width:200px;" /><span class="glyphicon glyphicon-qrcode form-control-feedback" style="right:120px;"></span><img class="verify" src="{:U(verify)}" alt="验证码" onClick="this.src=this.src+'?'+Math.random()" /></div><div class="row"><div class="col-xs-8"><div class="checkbox icheck"><label><input type="checkbox" name="agree" value="1"> 我同意<a href="#">网站安全协议</a></label></div></div><!-- /.col --><div class="col-xs-4"><button type="submit" class="btn btn-primary btn-block btn-flat">点击注册</button></div><!-- /.col --></div></form><a href="login.html" class="text-center">我已经注册了账户</a></div>。