<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.hmoore.net/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