[TOC]
## 完整代碼
>注:注釋的部分,可根據實際情況進行去掉!
```
<?php
namespace app\agent\controller;
use think\Controller;
class PublicController extends Controller
{
/**
* 后臺登陸界面
*/
public function login()
{
// $loginAllowed = session("__LOGIN_BY_CMF_ADMIN_PW__");
// if (empty($loginAllowed)) {
// $this->error('非法登錄!', cmf_get_root() . '/');
// }
$agent_id = session('AGENT_ID');
if (!empty($agent_id)) {//已經登錄
redirect(url("agent/Index/index"));
} else {
// $site_admin_url_password = config("cmf_SITE_ADMIN_URL_PASSWORD");
// $upw = session("__CMF_UPW__");
// if (!empty($site_admin_url_password) && $upw != $site_admin_url_password) {
// redirect(ROOT_PATH . "/");
// } else {
// session("__SP_ADMIN_LOGIN_PAGE_SHOWED_SUCCESS__", true);
// $result = hook_one('admin_login');
// if (!empty($result)) {
// return $result;
// }
return $this->fetch(":login");
// }
}
}
}
```
## `session`說明
| session名字 | 作用 |
| --- | --- |
| `__LOGIN_BY_CMF_ADMIN_PW__` | 設置后臺登錄加密碼,如果設置后值為1 |
| `AGENT_ID` | 登錄ID |
| `cmf_SITE_ADMIN_URL_PASSWORD` | 這個不清楚 |
| `__CMF_UPW__` | 這個不清楚 |
| `__SP_ADMIN_LOGIN_PAGE_SHOWED_SUCCESS__` | 這個不清楚 |
## 解析
### 簡單登錄
>本身登錄應該是簡簡單單的
```
<?php
namespace app\agent\controller;
use think\Controller;
class PublicController extends Controller
{
/**
* 后臺登陸界面
*/
public function login()
{
$agent_id = session('AGENT_ID');
if (!empty($agent_id)) {//已經登錄
redirect(url("agent/Index/index"));
} else {
return $this->fetch();
}
}
}
```
### 驗證登錄
>但是為了更安全一些,后臺設置了 `后臺加密碼`
```
<?php
namespace app\agent\controller;
use think\Controller;
class PublicController extends Controller
{
/**
* 后臺登陸界面
*/
public function login()
{
$loginAllowed = session("__LOGIN_BY_CMF_ADMIN_PW__");
if (empty($loginAllowed)) {
$this->error('非法登錄!', cmf_get_root() . '/');
}
$admin_id = session('AGENT_ID');
if (!empty($admin_id)) {//已經登錄
redirect(url("agent/Index/index"));
} else {
return $this->fetch();
}
}
}
```
### 后臺加密碼前端相關
#### 后臺加密碼代碼
```
<div class="form-group">
<label for="input-admin_url_password" class="col-sm-2 control-label">后臺加密碼</label>
<div class="col-md-6 col-sm-10">
<input type="text" class="form-control" id="input-admin_url_password" name="admin_settings[admin_password]" value="{$admin_settings.admin_password|default=''}" id="js-site-admin-url-password">
<p class="help-block">英文字母數字,不能為純數字</p>
<if condition="!empty($admin_settings.admin_password)">
<p class="help-block" style="color: red;">設置加密碼后必須通過以下地址訪問后臺,請勞記此地址,為了安全,您也可以定期更換此加密碼!</p>
<php>
$site_admin_url_password =config("SP_SITE_ADMIN_URL_PASSWORD");
$root=cmf_get_root();
$root=empty($root)?'':'/'.$root;
$site_domain = cmf_get_domain().$root;
</php>
<p class="help-block">后臺登錄地址:<span id="js-site-admin-url">{:$site_domain}/{$admin_settings.admin_password}</span></p>
</if>
</div>
</div>
```
#### 后臺加密碼圖片

非法登錄

#### 后臺加密碼路由
點擊保存后,會在 `data/route.php` 生成一個路由設置
```
<?php return array (
'test$' => 'admin/Index/index',
);
```
如圖所示:

這時我們訪問 `http://thinkcmf/admin` 或者 `http://thinkcmf/test` 就者可以訪問了!

這個值是在這里進行設置的!!
后臺登錄地址也很明顯可以訪問

### 備注二
```
$site_admin_url_password = config("cmf_SITE_ADMIN_URL_PASSWORD");
$upw = session("__CMF_UPW__");
```
這個應該是沒有用的!
### 備注三:hook
```
$result = hook_one('admin_login');
if (!empty($result)) {
return $result;
}
```
如果想要在登錄時加載一個 `hook`鉤子插件,可以在這里加載一個這樣的代碼
