如何一次仅从一台计算机使用一个ID/通行证登录


How to login with one ID/Pass from one computer at a time only

我有一个带有注册/登录系统的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,并将会话存储在数据库中(甚至更安全)。你还可以更容易地检查用户会话是否过期,以再次将signin列更新为0,这对不使用注销功能的用户来说更防水。。。(jycr753升级版解决方案)

过去对我有用的就是这个。

在用户表中添加一列以存储会话id。

当用户进行身份验证时,使用会话更新用户记录以进行登录。

当您检查请求是否经过身份验证时,请在数据库中搜索用户的会话id。

请注意,如果每个页面上都出现session_regenerate_id,则您必须根据每个请求更新用户记录,另一种选择是在会话中存储一个唯一值,并在用户记录上更新该值,然后通过匹配该值来确认已验证的请求。

在伪码中

session_start();
session_regenerate_id();
if ($_SESSION["session_key"]);
$user = UserStore::getUserBySessionKey($_SESSION["session_key"]);
if ($user){
    // the request is authenticated
} else {
    // redirect to the login page
}

登录时

session_start();
session_regenerate_id();
$user = UserStore::authenticateUser($username, $password);
if ($user){
     $sessionKey = uniqid().$user->username;
     $user->setSessionkey($sessionKey);
     $user->save();
     $_SESSION["session_key"] = $sessionKey;
} else {
    // show the login form with an error
}