第一章對登錄講解的超級詳細。
主要的功能歸類一下:
登錄驗证
設置了session
在配置文档中設置了session的失效時間
在login.html中有用到引用public文档夾下的css跟js的路徑的寫法。
退出登錄,清空session,再次http://127.0.0.1/login-demo/public/index.php/index/admin/index 是進不了後台的。
http://127.0.0.1/login-demo/public/index.php/index/login/login
新建數據庫跟插入數據 數據庫用的是MySQL,如果有需要,可以提供NoSQL的數據庫的登錄demo(mongodb)。
這裡密碼跟用戶名都是admin 下面是Sql語句,動動你的手指複制粘貼一下就可以了。
CREATE TABLE `admin` ( `id` int(11) NOT NULL, `admin_name` varchar(255) NOT NULL, `admin_password` varchar(255) NOT NULL, `admin_mail` varchar(255) NOT NULL ) INSERT INTO `admin` (`id`, `admin_name`, `admin_password`, `admin_mail`) VALUES (1, 'admin', '21232f297a57a5a743894a0e4a801fc3', 'admin@wkwkk.com');
新增了修改密碼功能,等驗证碼一起做好再發布上來,先把修改密碼的代碼貼出來:
下面是app/common/model/Admin.php
<?php namespace app\common\model; use think\Input; class Admin extends \think\Model { public static function login($name, $password) { $where['admin_name'] = $name; $where['admin_password'] = md5($password); $user=Admin::where($where)->find(); if ($user) { unset($user["password"]); session("ext_user", $user); return true; }else{ return false; } } // 退出登錄 public static function logout(){ session("ext_user", NULL); return [ "code" => 0, "desc" => "退出成功" ]; } // 查詢一條數據 public static function search($name){ $where['admin_name'] = $name; $user=Admin::where($where)->find(); return $user; // dump($user['admin_password']); } // 更改用戶密碼 public static function updatepassword($name,$newpassword){ $where['admin_name'] = $name; $user=Admin::where($where)->update(['admin_password' => md5($newpassword)]); if ($user) { return true; }else{ return false; } } }
下面是在view/admin下面新增的一個changepsw.html文档的代碼
<!DOCTYPE html> <html> <head> <title>修改密碼</title> </head> <body> <form method="post" action="changepassword"> 原密碼:<input type="password" name="oldpassword"></input> 新密碼:<input type="password" name="newpassword"></input> 再次輸入新密碼:<input type="password" name="newpassword1"></input> <input type="submit" value="提交"></input> </form> </body> </html>
下面是app/index/controller/Admin.php的代碼
<?php namespace app\index\controller; use \think\View; use think\Controller; class Admin extends Controller { public function index() { if (!session('?ext_user')) { header(strtolower("location: ".config("web_root")."/index/login/login")); exit(); } $view = new View(); return $view->fetch('index'); } // 退出登錄 public function logout(){ \app\common\model\Admin::logout(); if (!session('?ext_user')) { header(strtolower("location: ".config("web_root")."/index/login/login")); exit(); } header(strtolower("location:login")); return NULL; } //修改密碼 public function changepsw(){ if (!session('?ext_user')) { header(strtolower("location: ".config("web_root")."/index/login/login")); exit(); } $view = new View(); return $view->fetch('changepsw'); } public function changepassword(){ $oldpassword = md5(input('request.oldpassword')); $newpassword = input('request.newpassword'); $newpassword1 = input('request.newpassword1'); $name=session('ext_user')['admin_name']; $changepsw=\app\common\model\Admin::search($name); // dump($changepsw['admin_password']); $password=$changepsw['admin_password']; if ($password==$oldpassword ) { if ($newpassword==$newpassword1) { $updatepassword=\app\common\model\Admin::updatepassword($name,$newpassword); if ($updatepassword) { session("ext_user", NULL); return $this->success('修改成功,請重新登錄', ''.config("web_root").'/index/login/login'); }else{ return $this->error("修改密碼失敗"); } }else{ return $this->error("兩次輸入密碼不一致"); } }else{ return $this->error("原密碼輸入錯誤"); } } }