我有一个带有注册/登录系统的 PHP 网站。出于安全原因,我希望用户一次只能从一台电脑登录。如果同一 ID/密码尝试从另一台电脑登录,而用户已使用该 ID/密码登录,则应生成错误消息。
请告诉我这怎么可能,最好的方法是什么?
我只使用 cookie...
<?php function sec_session_start() {
$session_name = 'sec_session_id'; // Set a custom session name
$secure = false; // Set to true if using https.
$httponly = true; // This stops javascript being able to access the session id.
ini_set('session.use_only_cookies', 1); // Forces sessions to only use cookies.
$cookieParams = session_get_cookie_params(); // Gets current cookies params.
session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly);
session_name($session_name); // Sets the session name to the one set above.
session_start(); // Start the php session
session_regenerate_id(true); // regenerated the session, delete the old one.
}
这是代码:
if(login($name, $password, $mysqli) == true) {
// Login success
if($name == 'admin'){
if($signin == 'zero'){
$stmt = $mysqli->prepare("UPDATE users SET signin = 'one' WHERE name = 'admin'");
$stmt->execute();
?>
<span class="TextFont"><br /><br />Welcome Admin! <a href="admin.php">Click here to access your Admin Panel</a>! <br /><br /> <a href="logout.php">Logout</a></span>
<?php }
else{
echo"You are already logged in from some other system! $signin";
}}
else{
// some PHP code
}
else{
// some PHP code
}
最佳答案
如果您还在用户表中添加一列 session_id 并将 session 存储在数据库中(甚至更安全),您还可以更轻松地检查用户 session 是否过期,以再次将登录列更新为 0,这样更防水针对不使用注销功能的用户...(升级的 jycr753 解决方案)
关于php - 如何一次仅从一台计算机使用一个 ID/通行证登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17457204/