1  建表

 

drop table if exists p40_member;
create table p40_member
(
id mediumint unsigned not null auto_increment comment 'Id',
username varchar(30) not null comment '用户名',
password char(32) not null comment '密码',
face varchar(150) not null default '' comment '头像',
jifen mediumint unsigned not null default '0' comment'积分',
primary key (id)
)engine=InnoDB default charset=utf8 comment '会员表';

  导入数据库

2 在后台创建模型

    

<?php
namespace Admin\Model;
use Think\Model;
class MemberModel extends Model
{
// 添加和修改管理员时使用的表单验证规则
protected $insertFields = array('username','password','cpassword','chkcode');
protected $updateFields = array('id','username','password','cpassword');

//注册验证
protected $_validate = array(
array('username', 'require', '用户名不能为空!', 1, 'regex', 3),
array('username', '1,30', '用户名的值最长不能超过 30 个字符!', 1, 'length', 3),
// 第六个参数:规则什么时候生效: 1:添加时生效 2:修改时生效 3:所有情况都生效
array('password', 'require', '密码不能为空!', 1, 'regex', 1),
array('cpassword', 'password', '两次密码输入不一致!', 1, 'confirm', 3),
array('username', '', '用户名已经存在!', 1, 'unique', 3),
array('chkcode', 'require', '验证码不能为空!', 1),
array('chkcode', 'check_verify', '验证码不正确!', 1, 'callback'),
);
// 为登录的表单定义一个验证规则
public $_login_validate = array(
array('username', 'require', '用户名不能为空!', 1),
array('password', 'require', '密码不能为空!', 1),
array('chkcode', 'require', '验证码不能为空!', 1),
array('chkcode', 'check_verify', '验证码不正确!', 1, 'callback'),
);
// 验证验证码是否正确
function check_verify($code, $id = ''){
$verify = new \Think\Verify();
return $verify->check($code, $id);
}
public function login()
{
// 从模型中获取用户名和密码
$username = $this->username;
$password = $this->password;
// 先查询这个用户名是否存在
$user = $this->where(array(
'username' => array('eq', $username),
))->find();
if($user)
{
if($user['password'] == md5($password))
{
// 登录成功存session
session('m_id', $user['id']);
session('m_username', $user['username']);
return TRUE;
}
else
{
$this->error = '密码不正确!';
return FALSE;
}
}
else
{
$this->error = '用户名不存在!';
return FALSE;
}
}

public function logout()
{
session(null);
}

}

 

3 在前台创建控制器

 

<?php
namespace Home\Controller;
use Think\Controller;
class MemberController extends Controller
{

//验证码
public function chkcode()
{
$Verify = new \Think\Verify(array(
'fontSize' => 30, // 验证码字体大小
'length' => 2, // 验证码位数
'useNoise' => TRUE, // 关闭验证码杂点
));
$Verify->entry();
}
public function login()
{
if(IS_POST)
{
$model = D('Admin/Member');
// 接收表单并且验证表单
if($model->validate($model->_login_validate)->create())
{
if($model->login())
{
$this->success('登录成功!', U('/'));
exit;
}
}
$this->error($model->getError());
}
// 设置页面信息
$this->assign(array(
'_page_title' => '登录',
'_page_keywords' => '登录',
'_page_description' => '登录册',
));
$this->display();
}

public function regist()
{
if(IS_POST)
{
$model = D('Admin/Member');
// 接收表单并且验证表单
if($model->create(I('post.'),1))
{
if($model->add())
{
$this->success('注册成功!', U('login'));
exit;
}
}
$this->error($model->getError());
}
// 设置页面信息
$this->assign(array(
'_page_title' => '注册',
'_page_keywords' => '注册',
'_page_description' => '注册',
));
$this->display();
}
public function logout()
{
$model = D('Admin/Member');
$model->logout();
redirect('/');
}
}

 

4   创建一个模板目录,复制模板

      替换页头页脚 

      验证码

<layout name="Common/layout" />
<link rel="stylesheet" href="__PUBLIC__/Home/style/login.css" type="text/css">

<!-- 页面头部 start -->
<div class="header w990 bc mt15">
<div class="logo w990">
<h2 class="fl"><a href="index.html"><img src="__PUBLIC__/Home/images/logo.png" alt="京西商城"></a></h2>
</div>
</div>
<!-- 页面头部 end -->

<!-- 登录主体部分start -->
<div class="login w990 bc mt10">
<div class="login_hd">
<h2>用户登录</h2>
<b></b>
</div>
<div class="login_bd">
<div class="login_form fl">
<form action="__SELT__" method="post">
<ul>
<li>
<label for="">用户名:</label>
<input type="text" class="txt" name="username" />
</li>
<li>
<label for="">密码:</label>
<input type="password" class="txt" name="password" />
<a href="">忘记密码?</a>
</li>
<li class="checkcode">
<label for="">验证码:</label>
<input type="text" name="checkcode" />
<img style="cursor:pointer;" onclick="this.src='<?php echo U('chkcode'); ?>#'+Math.random();" src="<?php echo U('chkcode'); ?>" />
<span>看不清?<a href="">换一张</a></span>
</li>
<li>
<label for="">&nbsp;</label>
<input type="submit" value="" class="login_btn" />
</li>
</ul>
</form>

<div class="coagent mt15">
<dl>
<dt>使用合作网站登录商城:</dt>
<dd class="qq"><a href=""><span></span>QQ</a></dd>
<dd class="weibo"><a href=""><span></span>新浪微博</a></dd>
<dd class="yi"><a href=""><span></span>网易</a></dd>
<dd class="renren"><a href=""><span></span>人人</a></dd>
<dd class="qihu"><a href=""><span></span>奇虎360</a></dd>
<dd class=""><a href=""><span></span>百度</a></dd>
<dd class="douban"><a href=""><span></span>豆瓣</a></dd>
</dl>
</div>
</div>

<div class="guide fl">
<h3>还不是商城用户</h3>
<p>现在免费注册成为商城用户,便能立刻享受便宜又放心的购物乐趣,心动不如行动,赶紧加入吧!</p>

<a href="regist.html" class="reg_btn">免费注册 >></a>
</div>

</div>
</div>
<!-- 登录主体部分end -->

 

注册页面

  复制主要页面模板,修改表单提交的方法

   修改页头页脚

    在模型添加密码验证规则 6--20位

  array('password', '6,20', '密码的值最长不能超过 20 个字符!', 1, 'length', 3),

   修改验证码

 添加注册协议

 array('must_click', 'require', '必须同意注册协议!', 1, 'regex', 3),

<li>
<label for="">&nbsp;</label>
<input type="checkbox" name="must_click" value="1" class="chb" checked="checked" /> 我已阅读并同意《用户注册协议》
</li>

 

// 添加和修改管理员时使用的表单验证规则
protected $insertFields = array('username','password','cpassword','chkcode','must_click');

 

在注册之前把密码加密

 

protected function _before_insert(&$data, $option)
{
$data['password'] = md5($data['password']);
}

 

 

//注册模板

<layout name="Common/layout" />
<link rel="stylesheet" href="__PUBLIC__/Home/style/login.css" type="text/css">

<!-- 页面头部 start -->
<div class="header w990 bc mt15">
<div class="logo w990">
<h2 class="fl"><a href="index.html"><img src="__PUBLIC__/Home/images/logo.png" alt="京西商城"></a></h2>
</div>
</div>
<!-- 页面头部 end -->

<!-- 登录主体部分start -->
<div class="login w990 bc mt10 regist">
<div class="login_hd">
<h2>用户注册</h2>
<b></b>
</div>
<div class="login_bd">
<div class="login_form fl">
<form action="__SELF__" method="post">
<ul>
<li>
<label for="">用户名:</label>
<input type="text" class="txt" name="username" />
<p>1-30位字符,可由中文、字母、数字和下划线组成</p>
</li>
<li>
<label for="">密码:</label>
<input type="password" class="txt" name="password" />
<p>6-20位字符,可使用字母、数字和符号的组合,不建议使用纯数字、纯字母、纯符号</p>
</li>
<li>
<label for="">确认密码:</label>
<input type="password" class="txt" name="cpassword" />
<p> <span>请再次输入密码</p>
</li>
<li class="checkcode">
<label for="">验证码:</label>
<input type="text" name="chkcode" />
<img style="cursor:pointer;" onclick="this.src='<?php echo U('chkcode'); ?>#'+Math.random();" src="<?php echo U('chkcode'); ?>" />
<span>看不清?<a href="">换一张</a></span>
</li>
<li>
<label for="">&nbsp;</label>
<input type="checkbox" name="must_click" value="1" class="chb" checked="checked" /> 我已阅读并同意《用户注册协议》
</li>
<li>
<label for="">&nbsp;</label>
<input type="submit" value="" class="login_btn" />
</li>
</ul>
</form>


</div>

<div class="mobile fl">
<h3>手机快速注册</h3>
<p>中国大陆手机用户,编辑短信 “<strong>XX</strong>”发送到:</p>
<p><strong>1069099988</strong></p>
</div>

</div>
</div>
<!-- 登录主体部分end -->