The recent use of the YII framework for the company to develop two small projects, accumulated some knowledge, here to summarize, so that the future projects to use, you can refer to.
About the user logon logout feature.
Cwebuser class, using this component can easily implement user login, you need to personalize the session variable, you can create a class to inherit cwebuser and rewrite the login () method, such as:
Public Function Login ($identity, $duration =0)
{
$this->setstate (' __userinfo ', Array (' __id ' = + $identity->getid (), ' __name ' = + $identity->getname (), ' _ _FirstName ' = $identity->getfirstname ()));
Parent::login ($identity, $duration);
}
Write Yii::app ()->user->logout (FALSE) if you do not want all sessions of the app to be deleted when you log off.
When you log in, you need to authenticate the user, and Yii uses the Useridentity class to handle the logic. The reference code is as follows:
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{
$rs 1 = Tmember::model ()->findbyattributes (Array (' EMAIL ' = + $this->username);
$rs 2 = Tmember::model ()->findbyattributes (Array (' PASSWORD ' = $this->password));
$rs 3 = Tmember::model ()->findbyattributes (Array (' EMAIL ' + $this->username, ' PASSWORD ' =>md5 ($this (password)));
if (! $rs 3) {
if (! $rs 1) {
$this->errorcode=self::error_username_invalid;
}else{
if (! $rs 2) {
$this->errorcode=self::error_password_invalid;
}
}
}else{
$this->errorcode=self::error_is_not_actived;
}
}
$this->terrorcode[' front ') = $this->errorcode;
Return! $this->terrorcode[' front '];
}
2. Cross-domain login with cookie and session
First, set up a good environment, such as setting up a virtual domain name.
A. First modify the Hosts file in the C:\Windows\System32\drivers\etc directory, open with Notepad, and add:
127.0.0.1 mc.meztalks.com 127.0.0.1 meztalks.com Remember to remove the front #
127.0.0.1 mc.meztalks.com
127.0.0.1 meztalks.com
127.0.0.1 test.meztalks.com
Steps to read
650) this.width=650; "class=" Exp-image-default "alt=" PHP local implementation multi-domain access: Apache Virtual Host Configuration "src=" http://b.hiphotos.baidu.com/ Exp/w=500/sign=b0a539164cc2d562f208d0edd71090f3/810a19d8bc3eb1353a6720cda51ea8d3fd1f4427.jpg "style=" border:0px ; font-family: ' Microsoft Yahei ', ' Microsoft Black ', Arial, ' song Body ', Sans-serif;line-height:28px;text-align:justify;white-space: Normal;background-color:rgb (255,255,255); "/>
B. open the Xampp\apache\conf\httpd.conf file and search for "Include conf/extra/httpd-vhosts.conf" to make sure that there are no # annotations before, which is to ensure that vhosts is introduced The virtual host configuration file. The effect is as follows:
# Virtual Hosts
Include "Conf/extra/httpd-vhosts.conf"
Httpd-vhosts.conf is turned on, default a httpd.conf default configuration is disabled (ensure that the httpd-vhosts.conf file also opens the virtual host configuration, see 3rd), the domain name to access this IP will all point to vhosts.conf The first virtual host in the.
C. Locate the Apache installation directory, such as: D:\wamp\bin\apache\apache2.4.9\conf\extra, edit httpd-vhosts.conf, set the project directory you want to configure,
<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>
The virtual domain name has been configured to do so. Next you can go to the topic.
Cookies can be accessed across two-level domain names, but sessions cannot be accessed across domains, so for single sign-on, there are a few scenarios that you can try:
Using a cookie to transmit session_id, the database is used to save session variables.
Cookies are accessible across two-level domain names and are easy to implement, as long as you set up domain, reference code:
' Session ' = = Array (
' Class ' = ' application.components.MCCDbHttpSession ',
' Cookieparams ' =>array (' domain ' = ' meztalks.com ', ' lifetime ' =>0, ' path ' = '/'),
' ConnectionID ' = ' db ',
' Sessiontablename ' = ' t_db_session ',
),
Using the database to save the session, refer to the Yii cdbhttpsession class, there is a concession, you can override the class inherits Cdbhttpsession, rewrite Opensession (), Readsession (), writesession (), Destroysession ().
This article is from the "Skin Love Eat Skin" blog, please make sure to keep this source http://piaipi.blog.51cto.com/9387252/1617611
Development Summary of Yii Project