ThinkPHP怎么配置路由,让后台隐藏起来!
2022-11-24 11:02:51
221
{{single.collect_count}}
ThinkPHP如何隐藏后台?下面本篇文章就来给大家介绍一下ThinkPHP巧用路由规则隐藏后台的方法,让您的网站更安全!

php入门到就业线上直播课:进入学习
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API调试工具:点击使用

众所周知,如果thinkphp框架的后台模块名为admin的话,可以直接使用http://域名/admin的方式访问管理员后台,这个访问方式很方便,但也存在很大的安全隐患,黑客很容易就猜到了你的后台,然后进行暴力破解后台。那有什么方法可以解决这个隐患呢?下面我们一起来探讨如何利用路由规则来修改后台路径,防止黑客知道我们的后台入口。网上有很多隐藏后台admin的教程,但真正好用的,还是这个路由规则法。

1.png

第一步、后台添加可以修改后台模块名的设置参数

1、

2.png

2、保存设置的关键代码,如下:

if(request()->isPost()) {$data=input('post.');//获取系统全部模块名$system_module = [];foreach (scandir(APP_PATH) as $dir) {if($dir == '.' || $dir == '..') {continue;}if(is_dir(APP_PATH.$dir)) {array_push($system_module, $dir);}}foreach ($data as $key => $vo) {if($key == 'admin_module' && $vo != 'admin' && in_array($vo, $system_module)) {$this->error('后台地址不能与现有系统模块名同名');}}}
登录后复制

注意事项:

  • admin_module 是我数据库里保存后台模块名的键
  • APP_PATH 是thinkphp5.0版本的常量,如果是其他版本,请自行修改。

第二步、在application/common.php里读取网站的配置信息

1、config数据表主要结构如下:

DROP TABLE IF EXISTS `config`;CREATE TABLE `config` (`id` int(11) NOT NULL AUTO_INCREMENT,`key` varchar(255) DEFAULT NULL,`val` text,PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
登录后复制

2、sysconfig($name)方法:根据键名获取对应的值

<?phpuse think\Cache;use app\common\model\Config;/** * 获取或配置系统参数 * @param string $name参数名称 * @return string */function sysconfig($name) {$config = Cache::get('config');if (empty($config)) {$config = Config::column('key,val');Cache::set('config',$config,1800);//缓存30分钟 }return isset($config[$name]) ? $config[$name] : '';}
登录后复制

补充:

1、如果只是个人使用的系统,不想那么麻烦的话,也可以直接在config.php里直接添加如下配置:

return [// +----------------------------------------------------------------------// | 应用设置// +----------------------------------------------------------------------// 后台模块名'admin_module' => 'myadmin',]
登录后复制

2、然后在项目里直接调用:

$admin_module = Config('admin_module');
登录后复制

第三步、路由设置 application/route.php

<?phpuse think\route;$route_config = ['index'=>'index/index',];//1.获取后台模块$admin_module = sysconfig('admin_module');if ($admin_module == '') {$admin_module = 'admin';}//2.设置后台路由if ($admin_module != 'admin') {$admin_route_config = [//路由禁止:原理是把它指到非登陆地址,在没有登陆情况下,跳转到404页面;'admin/$' => 'admin/login/jump','admin/login$' => 'admin/login/jump','admin/login/index' => 'admin/login/jump',$admin_module . '/$' => 'admin/login/index',];$route_config = array_merge($route_config, $admin_route_config);}return $route_config;
登录后复制

第四步、在登录控制器Login.php里添加跳转验证的jump()方法

1、这个jump()方法实际上就是我们第三步禁止路由的指定方法

public function jump() {if(!Session::has('uid')) {$request = Request::instance();if(sysconfig('admin_module') == 'admin' || sysconfig('admin_module') == '') {$this->redirect('@admin/login/index');} else {header("HTTP/1.1 404 Not Found");return $this->fetch(APP_PATH.'/404.html');}} else {$this->redirect('@admin/index/index');}}
登录后复制

2、上面jump()里的代码,实现功能只有一个,那就是在未登录的情况下,访问被禁止的路由,会跳转到404页面,如下:

3.png

3、404.html页面放到application目录,代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>很抱歉,此页面暂时找不到!</title><style type="text/css">body {margin: 0px; padding:0px; font-family:"微软雅黑", Arial, "Trebuchet MS", Verdana, Georgia,Baskerville,Palatino,Times; font-size:16px;}div{margin-left:auto; margin-right:auto;}a {text-decoration: none; color: #1064A0;}a:hover {color: #0078D2;}img { border:none; }h1,h2,h3,h4 {/*display:block;*/margin:0;font-weight:normal; font-family: "微软雅黑", Arial, "Trebuchet MS", Helvetica, Verdana ; }h1{font-size:44px; color:#0188DE; padding:20px 0px 10px 0px;}h2{color:#0188DE; font-size:16px; padding:10px 0px 40px 0px;}#page{width:910px; padding:20px 20px 40px 20px; margin-top:80px;}.button{width:180px; height:28px; margin-left:0px; margin-top:10px; background:#009CFF; border-bottom:4px solid #0188DE; text-align:center;}.button a{width:180px; height:28px; display:block; font-size:14px; color:#fff; }.button a:hover{ background:#5BBFFF;}</style></head><body><div id="page" style="border-style:dashed;border-color:#e4e4e4;line-height:30px;"><h1>抱歉,找不到此页面~</h1><h2>Sorry, the page you're trying to find has moved. </h2><font color="#666666">你请求访问的页面,暂时找不到!</font><br /><br /><div class="button"><a href="javascript:;" onClick="javascript :history.back(-1);" title="返回上一页">返回上一页</a></div></div></body></html>
登录后复制

4、退出登录的方法

public function logout() {if(Session::has('adminid')) {Session::delete('adminid');}$this->redirect(url('@'.sysconfig('admin_module')));}
登录后复制

原文地址:https://juejin.cn/post/6981428649765371940

更多编程相关知识,请访问:编程入门!!

以上就是ThinkPHP怎么配置路由,让后台隐藏起来!的详细内容,更多请关注php中文网其它相关文章!

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}