think php 登录 (session+验证码)

时间:2021-08-16
本文章向大家介绍think php 登录 (session+验证码),主要包括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();

    }
}

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

原文地址:https://www.cnblogs.com/xiaoyantongxue/p/15149979.html