Yii項目開發總結

最近使用Yii框架給公司開發了兩個小型項目,積累了一些知識,在此進行彙總一下,以便將來項目用到時,可以參考。


  1. 關於用戶登錄註銷功能。

    1. CWebUser class,使用這個組件能輕鬆實現用戶登錄,需要個性化設置session變量時,可以創建一個類繼承CWebUser 且重寫 login()方法,比如:


      public function login($identity, $duration=0) 

        {     

              $this->setState('__userInfo', array('__id'=>$identity->getId(),'__name'=>$identity->getName(),'__firstName'=>$identity->getFirstName()));

              

              parent::login($identity, $duration);  

             

        }

    2. 註銷時,如果不希望將該應用的所有session都刪除,可以這樣寫Yii::app()->user->logout(false);

 登錄時,需要驗證用戶身份,Yii中使用UserIdentity類來處理該邏輯。參考代碼如下:

  

public function Authenticate()

    {

        $member = TMember::model()->findByAttributes(array('EMAIL'=>$this->username,'PASSWORD'=>md5($this->password),'IS_ACTIVED'=>1));

                

                if($member)

                {                                          

                        $this->setId($member->ID);

                        $this->setName($member->EMAIL);      

                        $this->setFirstName($member->FIRST_NAME);

                        $this->setPassword($this->password);

                        $this->errorCode=self::ERROR_NONE;

                    

                }else{

                    $rs1 = TMember::model()->findByAttributes(array('EMAIL'=>$this->username));

                    $rs2 = TMember::model()->findByAttributes(array('PASSWORD'=>$this->password));

                    $rs3 = TMember::model()->findByAttributes(array('EMAIL'=>$this->username,'PASSWORD'=>md5($this->password)));

                     if(!$rs3){

                    if(!$rs1){

                        $this->errorCode=self::ERROR_USERNAME_INVALID;

                    }else{

                        if(!$rs2){

                          $this->errorCode=self::ERROR_PASSWORD_INVALID;  

                        }

                      }                        

                    }else{

                        $this->errorCode=self::ERROR_IS_NOT_ACTIVED;

                    }

                }

                

                 $this->tErrorCode['front'] = $this->errorCode;

                return !$this->tErrorCode['front'];

    }


2.  採用cookie以及session實現跨域登錄

   首先,要搭建好環境,比如設置虛擬域名。


   a. 首先修改C:\Windows\System32\drivers\etc目錄下的 hosts 文件,用記事本打開,加入:

      127.0.0.1 mc.meztalks.com 127.0.0.1 meztalks.com記得去掉前面的#


     127.0.0.1 mc.meztalks.com

     127.0.0.1 meztalks.com

     127.0.0.1 test.meztalks.com

步驟閱讀

PHP本地實現多域名訪問:Apache虛擬主機配置

 







b.打開xampp\apache\conf\httpd.conf文件,搜索 “Include conf/extra/httpd-vhosts.conf”,確保前面沒有 # 註釋符,也就是確保引入了 vhosts 虛擬主機配置文件。效果如下:

# Virtual hosts

Include "conf/extra/httpd-vhosts.conf"

開啓了httpd-vhosts.conf,默認a的httpd.conf默認配置失效(確保 httpd-vhosts.conf 文件裏也開啓了虛擬主機配置,見第3條),訪問此IP的域名將全部指向 vhosts.conf 中的第一個虛擬主機。


c. 定位apache安裝目錄,如:D:\wamp\bin\apache\apache2.4.9\conf\extra,編輯httpd-vhosts.conf, 設置想要配置的項目目錄,

<VirtualHost *:80>

    ServerAdmin [email protected]

    DocumentRoot "E:/eztalks/eztalks-com/"

    ServerName meztalks.com

#ErrorLog "logs/dummy-host2.example.com-error.log"

#CustomLog "logs/dummy-host2.example.com-access.log" common

</VirtualHost>


<VirtualHost *:80>

    ServerAdmin [email protected]

    DocumentRoot "E:/eztalks/meetingcenter/"

    ServerName mc.meztalks.com

    ServerAlias mc.meztalks.com

DirectoryIndex index.php index.html index.htm

   #ErrorLog "logs/dummy-host.example.com-error.log"

   #CustomLog "logs/dummy-host.example.com-access.log" common

</VirtualHost>

 

虛擬域名到此已經配置好了。接下來可以進入主題。


cookie可以跨二級域名訪問,但session不能跨域訪問,因此想要實現單點登錄,可以嘗試的方案有:


採用cookie傳session_id,採用數據庫保存session變量。


cookie跨二級域名訪問,很容易實現,只要設置domain即可,參考代碼:


'session' => array (

              'class' => 'application.components.MCCDbHttpSession',

                                     'cookieParams'=>array('domain'=>'.meztalks.com','lifetime'=>0,'path'=>'/'),

                    'connectionID' => 'db',

                    'sessionTableName' => 't_db_session',

                ),

採用數據庫保存session,參考Yii自帶的CDbHttpSession類,有特許需要,可以重寫該類繼承CDbHttpSession,重寫openSession(),readSession(),writeSession(),destroySession().

  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章