<?php
/**
* cookie/session处理
* 会员注册验证
*/
//(1) 必须先有表
create table if not exists user (
user int unsigned auto_increment primary key,
username char(30) not null default '',
email char(30) not null default '',
password char(32) not null default '',
salr cahr(8) not null default '',
key username(username),
key email(email)
)engine myisam charset utf8;
/**
* V层
* (2) 修改html模板数据字段名,以及提交地址
*/
<form action = "{:U('Home/user/reg')}" method = "post">
/**
* M层
* (3) 验证提交数据的合法性
* 3.1 Model层声明验证方式
*/
class UserModel extends Model{
public $_validate = array(
//array(验证字段,验证规则,错误提示,【验证条件,附加条件,验证时间】)
array('username' , '3,10' , '用户名长度必须3到10位' , 1 ,'length', 3),//验证条件1为必须验证,2值不为空时验证,0存在时验证
array('email' , 'email' , '邮箱不合法' , 1, 'regex' , 3),//regex正则表达式验证
array('password' , '3,12' , '密码长度必须3到12位' , 1 , 'lenght' , 3),
array('re_password' , 'password' , '两次密码不一致' , 1 , 'confirm' , 3)//验证时间3表示全部情况下验证
);
}
/**
* C层
* (4)进行验证的C层判断
*/
pubcli function reg(){
if(!IS_POST){//若没有数据提交则展示模板
$this->display();
}else{//若有,则实例化链接表,调用create()方法验证
$userModel=D('user');
if(!$userModel->create()){//create()方法和$_validate进行搭配
echo $userModel->getError();
}
}
}
/**
* C层
* (5)将注册的密码进行加盐
* 5.1 用随机数生成盐
*/
public function yan(){
return mt_rand();
}
/**
* C层
* (5.2)以上验证成功后,调用盐,提供给POST传递过来的密码使用
*/
public function reg(){
if(!IS_POST){
$this->display();
}else{
$userModel=D('user');
if(!$userModel->create()){//若验证失败则报error
echo $userModel->getError();
}else{
$yan=$this->yan();//调用盐方法
//将post传递过来的password的名文密码加盐传递给password
$userModel->password=md5($userModel->password . $yan);
//将生成的盐也加入到对应表中
$userModel->salt=$yan;
if($userModel->add()){//若数据添加成功,则跳转到登陆方法
$this->redirect('Home/User/login');
}
}
}
}
?>}