You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wastesortingserver/admin/Login.php

89 lines
2.8 KiB

<?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=?", null, "sss", $manager->getLastIp(), $manager->getLastTime(), $manager->getManagerName());
// 跳转到首页
if ($_SERVER["SCRIPT_NAME"] == "/admin/Manager.php") {
5 years ago
info_res($msg, "/",);
echo <<<EOF
<script>
<!--3;秒后自动跳转;-->
$(function() {
let count=3;
$("#tip").text(count+"秒后自动跳转");
5 years ago
let i=setInterval(function() {
if(count-->0){
$("#tip").text(count+"秒后自动跳转");
}else{
location.href="/"
}
console.info("定时器!!!!!")
},1000)
})
5 years ago
</script>
<div class="alert alert-info alert-dismissible fade show col-5 m-auto text-center" role="alert">
<strong id="tip"></strong>
</div>
EOF;
} else if ($_SERVER["SCRIPT_NAME"] == "/index.php" && empty($_SERVER["HTTP_REFERER"])) {
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();
try {
DbUtil::query("select * from manager where manager_name=?", $manager_result, array($_COOKIE["manager_name"]));
} catch (Exception $e) {
error_res("系统出现严重异常,请联系管理员", "/");
return false;
}
// 验证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;
}