tp5框架实现登录功能

TP5框架实现登录功能

安装TP框架

使用最简单的安装方式,直接从官网下载解压,将压缩包里的文件复制到项目目录下。
管网地址:http://www.thinkphp.cn/down.html
安装完框架的目录如图所示
目录图

添加控制器

在application\index\controller目录下新建Login.php,主要用来实现登录功能的业务逻辑。代码如下:

<?php
namespace app\Index\controller;
use think\Controller;
use app\index\model\Admin;
class Login extends Controller
{
	public function index()
	{
		return $this->fetch('login');
	}
}      

创建登录模型

在这我用的是事先准备好的自适应的登录页面只需要将HTML文件、CSS文件和JS文件导入相应的位置并且配置常亮用来引入样式的文件路径。(需要登录页面模板的可以评论邮箱)

  1. 将login.html放入index模块下view\login文件下。如果没有此文件新建相应文件即可。
  2. 将准备好的fonts、style文件放入public\static\index目录下(如果没有此文件新建相应文件即可)。
  3. 导入好的目录为:
    在这里插入图片描述
    导入完文件之后的页面是:
    首次登录页面
    现在虽然我们可以显示页面但是页面的样式还没有成功引入进来。

配置常亮引入样式文件

  1. 在public文件下的index.php中定义常量
define('SITE_URL','http://127.0.0.1/tptest');
  1. 在application\index下新建配置文件config.php,并且配置常量。
<?php
return [
	'view_replace_str' =>[
		'__PUBLIC__'=>SITE_URL.'/public/static/index',
	],
];
  1. 在login.html里面添加常量引入样式文件,代码:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><!--Head--><head>
    <meta charset="utf-8">
    <title>登录</title>
    <meta name="description" content="login page">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!--Basic Styles-->
    <link href="__PUBLIC__/style/bootstrap.css" rel="stylesheet">
    <link href="__PUBLIC__/style/font-awesome.css" rel="stylesheet">
    <!--Beyond styles-->
    <link id="beyond-link" href="__PUBLIC__/style/beyond.css" rel="stylesheet">
    <link href="__PUBLIC__/style/demo.css" rel="stylesheet">
    <link href="__PUBLIC__/style/animate.css" rel="stylesheet">
</head>
<!--Head Ends-->
<!--Body-->

<body>
    <div class="login-container animated fadeInDown">
        <form action="" method="post">
            <div class="loginbox bg-white">
                <div class="loginbox-title">SIGN IN</div>
                <div class="loginbox-textbox">
                    <input value="admin" class="form-control" placeholder="username" name="username" type="text">
                </div>
                <div class="loginbox-textbox">
                    <input class="form-control" placeholder="password" name="password" type="password">
                </div>
                <div class="loginbox-submit">
                    <input class="btn btn-primary btn-block" value="Login" type="submit">
                </div>
            </div>
                <div class="logobox">
                    <p class="text-center">第一个登录页面</p>
                </div>
        </form>
    </div>
    <!--Basic Scripts-->
    <script src="__PUBLIC__/style/jquery.js"></script>
    <script src="__PUBLIC__/style/bootstrap.js"></script>
    <script src="__PUBLIC__/style/jquery_002.js"></script>
    <!--Beyond Scripts-->
    <script src="__PUBLIC__/style/beyond.js"></script>




</body><!--Body Ends--></html>
  1. 填完之后的页面是:
    登录页

后台代码实现

  1. 数据处理M层,在index模块下model文件夹下新建Admin.php,代码如下:
namespace app\index\model;
use think\Model;
use think\Db;
class Admin extends Model
{
	public function login($data){
		$user=Db::name('admin')->where('username','=',$data['username'])->find();
		if($user){
			if($user['password'] == md5($data['password'])){
				session('username',$user['username']);
				session('uid',$user['id']);
				return 3; //信息正确
			}else{
				return 2;//密码错误
			}
		}else{
			return 1;//用户不存在
		}
	}
}
  1. C层修改代码实现业务逻辑,Login.php代码如下:
<?php
namespace app\Index\controller;
use think\Controller;
use app\index\model\Admin;
class Login extends Controller
{
	public function index(){
		if(request()->isPost()){
			$admin=new Admin();
			$data=input('post.');
			$num=$admin->login($data);
			if($num==3){
				$this->success('信息正确,正在为您跳转','index/index');
			}else{
				$this->error('用户名或密码错误');
			}
		}
	}
	return $this->fetch('login');
}

创建数据库,配置数据库

根据自己的需求建立数据表连接,并在配置文件里配置数据库。就可以实现登录功能了。

如果想获取数据库文件或页面模板可以评论邮箱,会私信发给你

猜你喜欢

转载自blog.csdn.net/qq_41028544/article/details/84726917
今日推荐