2013-07-03 15 views
0

我有一個註冊/登錄系統的PHP網站。出於安全原因,我希望用戶一次只能從一臺PC登錄。如果在用戶使用該ID /密碼登錄時相同的ID /密碼嘗試從另一臺PC登錄,則應該生成錯誤消息。如何一次只能從一臺計算機登錄一個身份證/通行證

請告訴我它是如何可能的,什麼是做到這一點的最好方法是什麼?

我只用餅乾...

<?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 
} 
+1

添加一個額外的行表,它是用戶登錄0或每次1個校驗值,如果是1返回false,否則可以登錄 – jycr753

+1

如果你共享一個db結構的樣本,並且你如何登錄將會很容易幫助 – jycr753

+0

@ jycr753!我在登錄時啓動安全會話並在PHP的每個頁面上使用它。請看看我剛剛添加的代碼,並讓我知道這些漏洞。並感謝您的解決方案,我可能會實施它。 – user2548130

回答

0

,如果你還在user表中添加一列SESSION_ID和您存儲會話在數據庫(更安全)如果您的用戶會話過期更新登入列0再次,這是對不使用signout功能的用戶更具備防水也可以查看更容易...(升級jycr753溶液)

0

過去對我有用的是這個。

添加列到你的用戶表來存儲會話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 
}