php+js实现单点登录
2020年08月14日 00:45:23阅读数:110
登录信息表
DROP TABLE IF EXISTS `fly_admin_login_info`;
CREATE TABLE `fly_admin_login_info` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`admin_id` int(11) unsigned DEFAULT NULL,
`email` varchar(80) DEFAULT NULL,
`create_time` int(11) unsigned DEFAULT NULL,
`update_time` int(11) unsigned DEFAULT NULL,
`is_login` tinyint(4) unsigned DEFAULT NULL COMMENT '1-已登陆,2-未登录',
`login_time` int(11) unsigned DEFAULT NULL COMMENT '登录时间',
`session_id` varchar(150) DEFAULT NULL COMMENT '登录session_id',
`login_ip` varchar(30) DEFAULT NULL COMMENT '登录IP',
`os` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4;
// 记录登录信息
$login_info_data = [
"session_id" => $admin["session_id"],
"login_time" => $admin["login_time"],
"is_login" => 1,
"email" => $admin["email"],
"os" => Request::param("os"),
"login_ip" => Request::ip(),
];
if (Db::table('fly_admin_login_info')->where(["admin_id" => $admin["id"]])->find())
{
$login_info_data["update_time"] = $admin["login_time"];
Db::table('fly_admin_login_info')->where(["admin_id" => $admin["id"]])->update($login_info_data);
}
else
{
$login_info_data["create_time"] = $admin["login_time"];
$login_info_data["admin_id"] = $admin["id"];
Db::table('fly_admin_login_info')->insert($login_info_data);
}
/**
* 检测是否当前登录是否有效 建议独立封装配合中间件使用
*
*/
public function is_login_validity(Request $request)
{
$adminLogin = session('adminLogin');
if (empty($adminLogin))
{
echo json_encode(["status" => false, "info" => "登录失效,请重新登录!", "url" => url('admin/Index/login')]); exit();
}
else
{
$login_info = Db::table('fly_admin_login_info')->where(["admin_id" => $adminLogin["id"]])->find();
if (isset($adminLogin["session_id"]))
{
if ($login_info["session_id"] === $adminLogin["session_id"])
{
echo json_encode(["status" => true]);
}
else
{
session('adminLogin', null);
echo json_encode(["status" => false, "info" => "您的账号在IP:". $login_info["login_ip"] . "登录,如果不是您本人登录,则您的密码可能已经泄露,请及时修改密码 !", "url" => url('admin/Index/login')]); exit();
}
}
else
{
// 兼容从前
// 记录登录信息
$login_info_data = [
"session_id" => $adminLogin["session_id"],
"login_time" => $adminLogin["login_time"],
"is_login" => 1,
"email" => $adminLogin["email"],
"login_ip" => Request::ip(),
];
if (Db::table('fly_admin_login_info')->where(["admin_id" => $adminLogin["id"]])->find())
{
$login_info_data["update_time"] = $adminLogin["login_time"];
Db::table('fly_admin_login_info')->where(["admin_id" => $adminLogin["id"]])->update($login_info_data);
}
else
{
$login_info_data["create_time"] = $adminLogin["login_time"];
$login_info_data["admin_id"] = $adminLogin["id"];
Db::table('fly_admin_login_info')->insert($login_info_data);
}
echo json_encode(["status" => true]);
}
}
}
js 一直跑 该环境属于iframe环境
var is_login_validity_msg = null;
xadmin.admin_login = setInterval(function(){
$.post("is_login_validity",{token:"token_@$%!"},function(res){
if (res.status === false)
{
if (!parent.is_login_validity_msg)
{
parent.is_login_validity_msg = res.info;
}
res.info = parent.is_login_validity_msg ? parent.is_login_validity_msg : res.info;
clearInterval(xadmin.admin_login);
layer.msg(res.info, {icon: 5, time: 1000*60*2}, function()
{
parent.location.reload();
window.location.href = res.url;
});
}
},'json');
},3000);
---------------------
本文著作权归作者所有。
商业转载请联系作者获得授权,非商业转载请注明出处。
来源地址:https://www.php.cn/blog/detail/23691.html
来源:php中文网(www.php.cn)
© 版权声明:转载请附上博文链接!
全部评论
文明上网理性发言,请遵守新闻评论服务协议0条评论
立即提交