parent
7d3681fcb3
commit
01b6ab4367
@ -0,0 +1 @@ |
||||
#php_value auto_prepend_file "Manager.php" |
@ -0,0 +1,60 @@ |
||||
<?php |
||||
|
||||
require_once $_SERVER["DOCUMENT_ROOT"] . "/database/Query.php"; |
||||
require_once $_SERVER["DOCUMENT_ROOT"] . "/database/DbUtil.php"; |
||||
require_once $_SERVER["DOCUMENT_ROOT"] . "/admin/Alert.php"; |
||||
|
||||
//IP地址转换 |
||||
function get_remote_ip() |
||||
{ |
||||
return $_SERVER['REMOTE_ADDR'] == "::1" ? "127.0.0.1" : $_SERVER['REMOTE_ADDR']; |
||||
} |
||||
|
||||
|
||||
function update_token(QueryManager $manager_result, $msg) |
||||
{ |
||||
// 通过验证,更新cookie信息 |
||||
$manager = $manager_result->getManagerResult(); |
||||
$manager->setLastIp(get_remote_ip()); |
||||
$manager->setLastTime(date(default_format)); |
||||
DbUtil::update("update manager set last_ip=?,last_time=? where manager_name=?", "sss", $manager->getLastIp(), $manager->getLastTime(), $manager->getManagerName()); |
||||
|
||||
// 跳转到首页 |
||||
if ($_SERVER["SCRIPT_NAME"] == "/admin/Manager.php") { |
||||
info_res($msg, "/admin/Login.php"); |
||||
} else { |
||||
response($msg, "alert-info"); |
||||
} |
||||
|
||||
// 登陆身份有效期半小时 |
||||
setcookie("login_token", $manager->loginToken(), time() + 60 * 15, "/"); |
||||
} |
||||
|
||||
function check_login() |
||||
{ |
||||
if (!empty($_COOKIE["login_token"]) and !empty($_COOKIE["manager_name"])) { |
||||
$manager_result = new QueryManager(); |
||||
DbUtil::query("select * from manager where manager_name=?", $manager_result, array($_COOKIE["manager_name"])); |
||||
// 验证cookie身份信息有效性 |
||||
if (empty($manager_result) or $_COOKIE["login_token"] != $manager_result->getManagerResult()->loginToken()) { |
||||
setcookie("manager_name", "", time() - 3600, "/"); |
||||
setcookie("login_token", "", time() - 3600, "/"); |
||||
// error_res("非法验证,请重新登陆", "/admin/Manager.php"); |
||||
} else { |
||||
try { |
||||
update_token($manager_result, "您已登录,自动跳转到首页"); |
||||
return true; |
||||
} catch (Exception $e) { |
||||
error($e); |
||||
// error_res("管理员身份验证异常,请联系管理员", "/admin/Manager.php"); |
||||
} |
||||
|
||||
} |
||||
} |
||||
// else { |
||||
// info_res("您尚未登陆,跳转到登陆页", "/admin/Manager.php"); |
||||
// } |
||||
return false; |
||||
} |
||||
|
||||
|
@ -0,0 +1,102 @@ |
||||
<?php |
||||
//管理员后台登陆 |
||||
if (file_exists("../config.php")) { |
||||
require_once "../config.php"; |
||||
} else { |
||||
require_once "config.php"; |
||||
} |
||||
|
||||
require_once __ROOT__ . "/head.php"; |
||||
|
||||
getMenu("管理员登陆"); |
||||
|
||||
require_once __ROOT__ . "/database/DbUtil.php"; |
||||
require_once __ROOT__ . "/database/Query.php"; |
||||
require_once __ROOT__ . "/Log.php"; |
||||
require_once __ROOT__ . "/admin/Alert.php"; |
||||
require_once __ROOT__ . "/admin/Login.php"; |
||||
|
||||
if (!check_login()) { |
||||
if (empty($_REQUEST)) { |
||||
echo <<<EOF |
||||
<!DOCTYPE html> |
||||
<html lang="zh"> |
||||
<script> |
||||
$(function() { |
||||
Array.prototype.filter.call($("form.needs-validation"), function(form) { |
||||
form.addEventListener('submit', function(event) { |
||||
if (form.checkValidity() === false) { |
||||
event.preventDefault(); |
||||
event.stopPropagation(); |
||||
} |
||||
form.classList.add('was-validated'); |
||||
}, false); |
||||
}); |
||||
}) |
||||
</script> |
||||
<body> |
||||
<div class="container py-5"> |
||||
<div class="d-flex justify-content-center align-items-center w-100"> |
||||
<form class="col-6 needs-validation" novalidate method="post" action="/admin/Manager.php"> |
||||
<div class="input-group mb-3"> |
||||
<div class="input-group-prepend"> |
||||
<span class="input-group-text">管理员账号</span> |
||||
</div> |
||||
<input type="text" class="form-control" placeholder="管理员账号" name="manager_name" required> |
||||
<div class="invalid-feedback"> |
||||
管理员账号不为空 |
||||
</div> |
||||
</div> |
||||
<div class="input-group mb-3"> |
||||
<div class="input-group-prepend"> |
||||
<span class="input-group-text">管理员密码</span> |
||||
</div> |
||||
<input type="password" class="form-control" placeholder="管理员密码" name="manager_pwd" required> |
||||
<div class="invalid-feedback"> |
||||
管理员密码不为空 |
||||
</div> |
||||
</div> |
||||
|
||||
<button class="btn btn-block btn-info">提交</button> |
||||
</form> |
||||
</div> |
||||
</div> |
||||
</body> |
||||
</html> |
||||
EOF; |
||||
|
||||
} |
||||
|
||||
//else if (!empty($_COOKIE["login_token"]) and !empty($_COOKIE["manager_name"])) { |
||||
// $manager_result = new QueryManager(); |
||||
// DbUtil::query("select * from manager where manager_name=?", $manager_result, array($_COOKIE["manager_name"])); |
||||
//// 验证cookie身份信息有效性 |
||||
// if (empty($manager_result) or $_COOKIE["login_token"] != $manager_result->getManagerResult()->loginToken()) { |
||||
// setcookie("manager_name", "", time() - 3600); |
||||
// setcookie("login_token", "", time() - 3600); |
||||
// error_res("非法验证,请重新登陆", "/admin/Manager.php"); |
||||
// } else { |
||||
// try { |
||||
// update_token($manager_result,"您已登录,自动跳转到首页"); |
||||
// } catch (Exception $e) { |
||||
// error($e); |
||||
// error_res("管理员身份验证异常,请联系管理员", "/admin/Manager.php"); |
||||
// } |
||||
// |
||||
// } |
||||
//} |
||||
else if (empty($_POST["manager_name"])) { |
||||
error_res(array("status" => false, "msg" => "管理员不能为空"), "/admin/Manager.php"); |
||||
} else if (empty($_POST["manager_pwd"])) { |
||||
error_res(array("status" => false, "msg" => "管理员密码不能为空"), "/admin/Manager.php"); |
||||
} else { |
||||
$manager_result = new QueryManager(); |
||||
DbUtil::query("select * from manager where manager_name=? and manager_pwd=?", $manager_result, array($_POST["manager_name"], md5($_POST["manager_pwd"]))); |
||||
if (empty($manager_result->getManagerResult())) { |
||||
error_res("登陆失败,管理员或密码错误", "/admin/Manager.php"); |
||||
} else { |
||||
update_token($manager_result, "验证成功,自动跳转到首页"); |
||||
setcookie("manager_name", $manager_result->getManagerResult()->getManagerName(), time() + 60 * 15, "/"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,96 @@ |
||||
<?php |
||||
|
||||
//管理员实体 |
||||
class Manager |
||||
{ |
||||
private $manager_name; |
||||
private $manager_pwd; |
||||
private $last_ip; |
||||
private $last_time; |
||||
|
||||
/** |
||||
* Manager constructor. |
||||
* @param $manager_name |
||||
* @param $manager_pwd |
||||
* @param $last_ip |
||||
* @param $last_time |
||||
*/ |
||||
public function __construct($manager_name, $manager_pwd, $last_ip, $last_time) |
||||
{ |
||||
$this->manager_name = $manager_name; |
||||
$this->manager_pwd = $manager_pwd; |
||||
$this->last_ip = $last_ip; |
||||
$this->last_time = $last_time; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getManagerName() |
||||
{ |
||||
return $this->manager_name; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getManagerPwd() |
||||
{ |
||||
return $this->manager_pwd; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getLastIp() |
||||
{ |
||||
return $this->last_ip; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getLastTime() |
||||
{ |
||||
return $this->last_time; |
||||
} |
||||
|
||||
public function loginToken() |
||||
{ |
||||
return md5($this->last_ip . $this->last_time); |
||||
} |
||||
|
||||
/** |
||||
* @param mixed $manager_name |
||||
*/ |
||||
public function setManagerName($manager_name): void |
||||
{ |
||||
$this->manager_name = $manager_name; |
||||
} |
||||
|
||||
/** |
||||
* @param mixed $manager_pwd |
||||
*/ |
||||
public function setManagerPwd($manager_pwd): void |
||||
{ |
||||
$this->manager_pwd = $manager_pwd; |
||||
} |
||||
|
||||
/** |
||||
* @param mixed $last_ip |
||||
*/ |
||||
public function setLastIp($last_ip): void |
||||
{ |
||||
$this->last_ip = $last_ip; |
||||
} |
||||
|
||||
/** |
||||
* @param mixed $last_time |
||||
*/ |
||||
public function setLastTime($last_time): void |
||||
{ |
||||
$this->last_time = $last_time; |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue