【问题描述】:同一用户在同一时间多次登录如果不能检测出来,是危险的。因为,你无法知道是否有其他用户在登录你的账户。如何禁止同一用户多次登录呢?
【解决方案】
(1) 每次登录,身份认证成功后,重新产生一个session_id
。
session_regenerate_id();
session_register("username");
(2) 在用户数据库中开一个sessionid
字段,重新产生session_id后,都更新该字段。
$sessionid = session_id();
$db = new PDO('sqlite:softToken.db');
$sql = "update userinfo set sessionid ='$sessionid' where username='$username' and passwd='$passwd';";
$query = $db->prepare($sql);
$query->execute();
(3) 建立一个
保存用户名session
$_SESSION["username"] = $username;
(4) 利用url重写,传递session_id
$url = "main.php?sid=".session_id();
unset($db);
echo "登录成功,正在跳转!" ;
header ("Location:$url");
(5) 在需要跳转的页面,起始处加入
header('Content-type:text/html; charset=utf-8');
$sessionid = $_GET['sid'];
session_id($sessionid);
session_start ();
$username = $_SESSION["username"];
$db = new PDO('sqlite:softToken.db');
$sql = "select * from userinfo where username='$username' and sessionid='$sessionid';";
$query = $db->prepare($sql);
$query->execute();
$user = $query->fetch(PDO::FETCH_OBJ);
if ($user->username == ""){
session_destroy();
echo "" ;
exit () ;
}
未经允许不得转载:任鹏个人博客 » PHP如何防止同一用户多次登录
最新评论
Forex wiki. https://lt.forex-stock-bitcoin-brokers.com
Magnificent items from you, man. I have take note your stuff
Following on from the 3rd March Meetings held by economic de
It is remarkable, rather valuable message dfgdlfg2131.32
一般都会有一个沙盒期的,过了沙盒期就会慢慢放出来
百度不收录是应为是新站的原因吗?
The spike in consumer prices that left inflation at a four-d