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

60 lines
2.1 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=?", "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;
}