PHP

tp5.x版本简单登录demo代码

字号+ 编辑: 国内TP粉 修订: 种花家 来源: ThinkPHP 2023-09-08 我要说两句(0)

tp5.0 简单登录demo 刚刚更新了这个demo,加了session,退出登录做好了,接下来将要加一个验证码功能跟修改密码功能。

第一章对登录讲解的超级详细。

主要的功能归类一下:

  1. 登录验证

  2. 设置了session

  3. 在配置文件中设置了session的失效时间

  4. 在login.html中有用到引用public文件夹下的css跟js的路径的写法。

  5. 退出登录,清空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("原密码输入错误");
      }
      
    }
}


阅完此文,您的感想如何?
  • 有用

    0

  • 没用

    0

  • 开心

    0

  • 愤怒

    0

  • 可怜

    0

1.如文章侵犯了您的版权,请发邮件通知本站,该文章将在24小时内删除;
2.本站标注原创的文章,转发时烦请注明来源;
3.交流群: PHP+JS聊天群

相关课文
  • mac开发接入微信公众号接口返回报错 cURL error 56: SSLRead() return error -9806

  • pecl安装程序时报错Array and string offset access syntax with curly braces is no longer supported

  • PHP的换行符是什么

  • 由于商家传入的H5交易参数有误,该笔交易暂时无法完成,请联系商家解决

我要说说
网上宾友点评