think php 登录 (session+验证码)

、、、、、、、、、、表单页面

复制代码
<!DOCTYPE html>
{__NOLAYOUT__}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>登录</title>
    <link rel="stylesheet" href="__STATIC__/admin/css/login.css">
</head>
<body>
<form action="/goods/login/save" method="post">
    <div class="login">
        <div class="center">
            <h1>Login</h1>
            <div class="inputLi">
                <strong>账户</strong>
                <input type="text" placeholder="账户" name="account">
            </div>
            <div class="inputLi">
                <strong>密码</strong>
                <input type="password" placeholder="密码" name="password">
            </div>
            <div class="inputLi">
                <strong>验证码</strong>
                <input type="text" placeholder="" name="cord">
                <img src="{:captcha_src()}"  name="img" onclick="this.src='{:captcha_src()}'"
                >
            </div>
            <div class="inputLi">
                <button type="submit">登录</button>
            </div>
        </div>
    </div>
</form>
</body>
</html>



复制代码

、、、、、、、、、、、、、、控制器验证‘

 

 

复制代码
  public function create()
    {
        //goods/login/create
        //通过域名/模块名/控制器名/方法名访问登录页面
        return view();
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $params = $request->param();
        //验证参数、非空
        $rule = [
            'account' => 'require',
            'password' => 'require',
            'cord' => 'require',
        ];
        $tips = [
            'account.require' => '账号不可以为空',
            'password.require' => '密码不可以为空',
            'cord.require' => '验证码不可以为空',
        ];
        $validate = new Validate($rule, $tips);
        $result = $validate->check($params);
        if (!$result) {
            $this->error($validate->getError());
        }
//数据库验证参数
        $data = GoodModel::login($params);
        //验证账号
        if ($params['account'] != $data['name']) {
            $this->error('账号错误', '/goods/login/create');
        }
        //验证密码
        if (md5($params['password']) != md5($data['password'])) {
            $this->error('密码错误', '/goods/login/create');
        }
        //验证码进行验证
        if (!captcha_check($params['cord'])) {
            $this->error('验证码输入错误', '/goods/login/create');
        };
        if ($data){
            //记录session
           session('name',$data['name']);
            $this->success('登录成功', '/goods/goods/index');
        }



    }
复制代码

。。。。。。。。。。。。。。。。模型页面

 

复制代码
<?php

namespace app\goods\model;

use think\Model;

class GoodModel extends Model
{
    //
    protected $table='login';
    public static function login($params){
       return self::where('name',$params['account'])->find();

    }
}
复制代码

。。。。。。。。。。。。。。。。。。。。

 

 

 

 

 

 

posted @   王越666  阅读(57)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示