簡體   English   中英

如何同步我的用戶連接PHP

[英]How do I sync my users connections PHP

我在PHP中有一個名為cUser的類:

class cUser {
    var $m_email;//The users email adresse(String)
    var $m_password;//His password(String)
    var $m_username;//His username(String)
    var $m_active;//If the user have been activate (By following a link send to him via is email)(Bool)

    function __construct($p_username, $p_password, $p_email, $p_active) {
        $this->m_username = $p_username;
        $this->m_password = $p_password;
        $this->m_email = $p_email;
        $this->m_active = $p_active;
    }

    //this is the important part...
    function connexion() {
        include "Config.php";//include all the parameters needed to connect to the DB

        $cn = new cConnexion($ConnexionDBHost, $ConnexionDBName, $ConnexionDBLogin, $ConnexionDBPassword);//Initiate a connection to the DB

        if($cn->DBConnexion())//If it is connected {
            $parameters = array('username'=>$this->getUsername(), 'password'=>$this->getPassword());//create an array with the username and the password
            $getConnexion = $cn->SecureSelect("SELECT username, password, email, active FROM user WHERE BINARY username = :username AND BINARY password = :password", $parameters);//selecte the user in the DB (for DB description see below code)

            if($getConnexion != null) { //if there is no error in the query.
                $resultSet = $getConnexion->fetch();//fetch the results
                if($resultSet != null) { //if there is a match
                    //assigne the DB field values to this instance of cUser
                    $this->setUsername($resultSet['username']);
                    $this->setPassword($resultSet['password']);
                    $this->setEmail($resultSet['email']);
                    $this->setActive($resultSet['active']);

                    if($this->getActive() == 1) {
                        //If the user has been activate already return success
                    }
                    else {
                        //Else send an activation email to the user.Dont connecte him and return an error message
                    }
                }
                else {
                    //Send an error message
                }
            }
            else {
                //send an error message
            }
        }
        else {
            //send an error message
        }
    }

    //this are not important for the question but I put them there so you can see what kind of operation the class is doing.
    function delete(){//Delete this instance of cUser from de DB}
    function insert(){//Insert this instance of cUser from the DB}
    function update($p_email, $p_username, ...){//Update this instance of cUser with the new parameters}
    function activateAccount(){//Activate this instance of cUser}
    //And all the getters and setters associate with the class attributes.
}

這是MySQL表,其中包含cUser類的字段(大致編碼):

USER
    varchar email,
    varchar password,
    varchar username,
    tiny int activate,//1 or 0
    tiny int connected//1 or 0

題:

如何實現或更改功能連接,以便同時連接一個用戶實例?

注意:

我已經知道我可以檢查DB Connected(連接數據庫)字段是否設置為1,但是如果兩個用戶同時訪問DB,則會造成問題(競賽條件或類似問題)。

是否可以使用諸如mutex或信號量之類的東西來同步對連接的數據庫字段的訪問?

例:

David填寫HTML表單,並使用用戶名和密碼(“ Dav1”,“ ThisIsPassword”)提交,一個過程頁面創建cUser實例並進行連接以檢查Dav1是否已存在,然后讓他訪問其余的Web應用程序。

現在,達沃斯填寫該表單,並使用大衛使用的用戶名和密碼提交該表單,因為達沃斯和大衛是朋友,他們共享相同的帳戶並在其中共享密碼。

使用現有的代碼,David和Davos都可以使用相同的帳戶同時訪問Web應用程序,我想要的是當David連接Davos時收到一條錯誤消息,告訴他該用戶已經連接或用戶名/ password不匹配。

使用交易。

在MySql中,您也可以使用SELECT FOR UPDATE語句。

偽代碼:

$transaction = db->beginTransaction();
try {
    $user = User::getByUsername($username);

    if ($passwordImcorrect)
        throw new Exception('invalid credentials');

    if (user->loggedIn)
        throw new Exception('already logged in');

    user->loggedIn = 1;
    user->save();

    $transaction->commit();
}
catch (Exception $e) {
    echo $e->getMessage();
    $transaction->rollback();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM