批改状态:合格
老师批语:
独立完成一个完整的用户登录与验证过程
<?php
namespace app\index\controller;
use app\index\validate\User as UserValidate;
use app\model\User as UserModel;
use think\facade\View;
use think\facade\Request;
use traits\controller\Jump;
use think\facade\Session;
class Demo4
{
use Jump;
public function login(){
if (Session::has('email') && !empty(Session::get('email'))){
$this->error('不要重复登陆','hello');
}
return View::fetch('login');
}
public function check(UserValidate $validate,UserModel $user){
$data = Request::param();
if ($validate->scene('login')->check($data)){
if ($user->checkUser($data)){
Session::set('email',$data['email']);
$this->success('登陆成功','hello');
}else{
$this->error('登陆错误','login');
}
}else{
$this->error($validate->getError(),'login');
}
}
public function hello(){
$email = Session::get('email');
$url = url('logout');
return View::display("<h3>恭喜登陆成功:{$email}</h3><a href='{$url}'>退出</a>");
}
public function logout(){
Session::delete('email');
$this->success('退出成功','login');
}
}点击 "运行实例" 按钮查看在线实例


总结,用框架来写,简单很多!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号