skymvc教学课程
第6课 skymvc用户登录注册的实现
/**********
*大家好,这节课给大家讲讲用户登录注册的实现。
*上节课我们讲到session及cookie.登录状态的保存,这节课我们实践下,用户模块设计。
*
*/
用户表
CREATE TABLE `sky_user` (
`userid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`nickname` varchar(16) not null DEFAULT '' comment '昵称',
`username` varchar(16) NOT NULL default '' comment '用户名',
`gender` tinyint(4) UNSIGNED NOT NULL DEFAULT '1' COMMENT '性别',
`user_head` varchar(255) NOT NULL DEFAULT '/static/img/head/2019.jpg' comment '头像',
`last_time` datetime NOT NULL DEFAULT '1988-02-01 12:12:12' comment '最后登录时间',
`salt` varchar(6) NOT NULL DEFAULT '' comment '加密串',
`password` varchar(64) NOT NULL DEFAULT '' comment '密码',
`reg_time` datetime NOT NULL DEFAULT '2016-05-11 14:53:01' COMMENT '注册时间',
PRIMARY KEY (`userid`),
key(username),
key(nickname)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 comment '用户表';
注册 register.ctrl.php
我们需要验证
1.用户名、昵称是否为空 是否已存在
2.两次输入密码是否一致
<?php class registerControl extends skymvc{ public function __construct(){ parent::__construct(); } public function onDefault(){ $this->smarty->display("register/index.html"); } public function onRegSave(){ $password=post('password','h'); $password2=post('password2','h'); if($password!=$password2 or empty($password)){ $this->goall("两次输入的密码不一致",1); } $data['username']=post('username','h'); if(empty($data['username']) ){ $this->goAll("用户名不能为空",1); } $data['nickname']=post('nickname','h'); if(empty($data['nickname']) ){ $this->goAll("昵称不能为空",1); } $data['gender']=post('gender','i'); if(M("user")->selectRow(array("where"=>"username='".$data['username']."' "))){ $this->goall("用户名已经存在",1); } if(M("user")->selectRow(array("where"=>"nickname='".$data['nickname']."' "))){ $this->goall("昵称已经存在",1); } $data['salt']=rand(1000,9999); $data['password']=umd5($password.$data['salt']); $userid=M("user")->insert($data); $this->goAll("注册成功,请登录",0,$user,"/index.php?m=login"); } } ?>
登录 login.ctrl.php
登录
查询用户名是否有
检查密码是否一致
注销
<?php class loginControl extends skymvc{ public function __construct(){ parent::__construct(); } public function onInit(){ } public function onDefault(){ $this->smarty->assign(array( "backurl"=>$_SERVER['HTTP_REFERER'] )); $this->smarty->display("login/index.html"); } public function onloginSave(){ $username=post('username','h'); $password=post('password','h'); $user=M("user")->selectRow("username='".$username."'"); if(!$user){ $this->goAll('账号不存在',1,"",$_SERVER['HTTP_REFERER']); } if($user['password']!=umd5($password.$user['salt'])){ $this->goall("密码出错了",1,"",$_SERVER['HTTP_REFERER']); } $this->set_session('ssuser',$user); $authcode=jiami($user['userid']."|".umd5($user['password'])); if(ISWAP or post('autologin')){ $this->set_cookie("authcode",$authcode,3600); } $redata=array( "authcode"=>$authcode ); $this->goAll("登录成功",0,$redata,get_post('backurl')); } public function onLogout(){ $this->del_session("ssuser"); $this->set_cookie("authcode","",time()-10); $this->goall("注销成功",0,0,"/index.php"); } } ?>