tp5.0 简单登录demo 已更新

浏览:26809 发布日期:2016/07/19 分类:系统代码 关键字: thinkphp5.0 登录 demo
tp5.0 简单登录demo 刚刚更新了这个demo,加了session,退出登录做好了,接下来将要加一个验证码功能跟修改密码功能。
希望大家多多支持我在看云发布的手册,第一章对登录讲解的超级详细。http://www.kancloud.cn/w113211/killall《基于ThinkPHP5.0的各项后台小功能代码实现》


ThinkPhP两个版本的权限管理(3.2.3和5.0)
https://www.kancloud.cn/w113211/alls

第一章对登录讲解的超级详细。
主要的功能归类一下:
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@ideait.net');
新增了修改密码功能,等验证码一起做好再发布上来,先把修改密码的代码贴出来:
下面是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("原密码输入错误");
      }
      
    }
}
[code][/code]

附件 login-demo.rar ( 545.34 KB 下载:1644 次 )

评论( 相关
后面还有条评论,点击查看>>