getConnection(); $sql = "UPDATE users SET session_id = :session_id WHERE user_id = :user_id"; $query = $database->prepare($sql); $query->execute(array(':session_id' => $sessionId, ":user_id" => $userId)); } /** * checks for session concurrency * * This is done as the following: * UserA logs in with his session id('123') and it will be stored in the database. * Then, UserB logs in also using the same email and password of UserA from another PC, * and also store the session id('456') in the database * * Now, Whenever UserA performs any action, * You then check the session_id() against the last one stored in the database('456'), * If they don't match then log both of them out. * * @access public * @static static method * @return bool * @see Session::updateSessionId() * @see http://stackoverflow.com/questions/6126285/php-stop-concurrent-user-logins */ public static function isConcurrentSessionExists() { $session_id = session_id(); $userId = Session::get('user_id'); if (isset($userId) && isset($session_id)) { $database = DatabaseFactory::getFactory()->getConnection(); $sql = "SELECT session_id FROM users WHERE user_id = :user_id LIMIT 1"; $query = $database->prepare($sql); $query->execute(array(":user_id" => $userId)); $result = $query->fetch(); $userSessionId = !empty($result)? $result->session_id: null; return $session_id !== $userSessionId; } return false; } /** * Checks if the user is logged in or not * * @return bool user's login status */ public static function userIsLoggedIn() { return (self::get('user_logged_in') ? true : false); } }