<div class="truth">
别以为自己是女生,就以为自己做不了程序。世界上第一个程序员 阿达·洛芙莱斯,就是个女士!
</div>
<blockquote class="default">
<p>知识点<br>
1.表单验证<br>
2.模型操作<br>
</p>
<div class="env">版本:thinkphp5.07</div>
</blockquote>
<div class="step">1.修改站点根目录</div>
<span class="info">已经在 《[登录认证](313753)》章节详细介绍
</span>
<div class="step">2.隐藏index.php</div>
<span class="info">已经在 《[登录认证](313753)》章节详细介绍
</span>
<div class="step">3.重新设置视图目录</div>
<span class="info">已经在 《[登录认证](313753)》章节详细介绍
</span>
<div class="step">4.创建用户表</div>
<span class="info">已经在 《[登录认证](313753)》章节详细介绍
</span>
<div class="step">5.链接mysql数据库</div>
<span class="info">已经在 《[登录认证](313753)》章节详细介绍
</span>
<div class="step">6.创建公共模板</div>
<span class="info">已经在 《[登录认证](313753)》章节详细介绍
</span>
<div class="step">7.注册模板</div>
<span class="info">注册模板跟登录模板一样呢,都是继承与auth公共模板,代码如下:
~~~
{extend name="auth" /}
{block name="title"}注册{/block}
{block name="auth-title"}用户注册{/block}
{block name="auth-form"}
<div class="form-group">
<label for="username">用户名:</label>
<input type="hidden" name="__token__" value="{$Request.token}" />
<input type="text" class="form-control" id="username" name="username" placeholder="用户名">
</div>
<div class="form-group">
<label for="password">密码:</label>
<input type="password" class="form-control" id="password" name="password" placeholder="密码">
</div>
<div class="form-group">
<label for="password">重复密码:</label>
<input type="password" class="form-control" id="repassword" name="repassword" placeholder="密码">
</div>
<div class="form-group">
<label for="password">昵称:</label>
<input type="text" class="form-control" id="nickname" name="nickname" placeholder="昵称">
</div>
<div class="form-group">
<label for="yzm">验证码:</label>
<input type="text" class="form-control" id="yzm" name="yzm" placeholder="验证码">
<div class="yzm"><img src="{:captcha_src()}" id="codeimage" alt="captcha" onclick="javascript:this.src = '__ROOT__/captcha.html?time=' + Math.random();" /></div>
</div>
{/block}
{block name="auth-submit"}
<button type="submit" class="btn btn-primary">注册</button>
<button type="reset" class="btn btn-info">重置</button>
<a class="btn btn-default" href="{:url('index/auth/register')}">登录</a>
{/block}
{block name="script"}
<script type="text/javascript">
$("#userform").validate({
rules: {
username: {
required: true,
},
password: {
required: true,
},
nickname: {
required: true,
},
yzm: {
required: true,
},
},
messages: {
username: {
required: "请输入用户名",
},
password: {
required: "请输入密码",
},
nickname: {
required: "请输入您的昵称",
},
yzm: {
required: "请输入验证码",
},
}
});
</script>
{/block}
~~~
</span>
<div class="step">8.后台表单验证</div>
<span class="info">
永远不要相信用户的数据,所以现在给表单提交添加数据验证。我们添加一个User验证器(位于application/index/validate/User.php),代码如下:<br/>
~~~
<?php
namespace app\index\validate;
use think\Validate;
class User extends Validate
{
// 验证规则
protected $rule = [
'username' => ['require'],
'password' => ['require'],
'nickname' => ['require'],
];
}
~~~
<p>这里只是一个简单的案例,其实表单验证插件很多,比如日期验证,最大最小验证,甚至是可以自定义验证函数,想要了解更详细的,请参考官方正版教材
http://www.kancloud.cn/thinkphp/thinkphp5_quickstart/147289
</p>
</span>
<div class="step">9.注册控制器</div>
<span class="info">
if ($user->allowField(true)->validate(true)->save(input('post.'))) {}这段代码allowField函数将清楚一些非字段类表单数据,validate会在保存的时候自动去验证。具体代码如下:
~~~
public function register()
{
if (input('post.')) {
// 首先验证验证码:
$captcha = new \think\captcha\Captcha();
if (!$captcha->check(input('post.yzm'))) {
//注意,如果session设置为二维数组,必须在赋值的时候,将他一维数组session清空。tp5session中存在bug,session不能当数组来使用!
session('message_info',null);
Session::set('message_info.title','注册失败');
Session::set('message_info.content','验证码错误');
$this->redirect(url('/index/auth/register'));
}
$user = new User;
if ($user->allowField(true)->validate(true)->save(input('post.'))) {
session('message_info',null);
Session::set('message_info.title','注册成功');
Session::set('message_info.content','请登录');
$this->redirect(url('/index/auth/login'));
} else {
session('message_info',null);
Session::set('message_info.title','注册失败');
Session::set('message_info.content',$user->getError());
$this->redirect(url('/index/auth/register'));
}
}
return $this->fetch();
}
~~~
</span>
---
至此,整个完整登录已经完成。
源码下载地址:https://pan.baidu.com/s/1gfuvePd
thinkphp5学习交流群:536633782