简体   繁体   English

普通php登录到YII登录

[英]Normal php login to YII login

i have a normal php login which connects to a database authenticates the user, now i need to convert this to a login that uses yii framework, can anybody tell me in order to do so.. what are the first things that i should do and can i convert this to yii login. 我有一个正常的php登录名,该登录名连接到数据库对用户进行身份验证,现在我需要将其转换为使用yii框架的登录名,任何人都可以告诉我这样做..我应该做的第一件事是我可以将其转换为yii登录。 following is the current login function that i have to call 以下是我必须调用的当前登录功能

function login($usr,$pwd) {
    $query = "SELECT * FROM login WHERE us.username='$usr' AND us.password='$pwd'; ";
    $dataReader=$command->query();
    $row = mysql_fetch_array($dataReader);
    $log = new stdClass();
    if($row) {
        $pro->accountID = (int)$row['accountID'];
        $pro->accountname = $row['accountname'];
        $pro->usertype = (int)$row['usertype'];
                $string = rand() . 'SURVAYLAND' . rand() . $usr. $pwd;
            $_SESSION['SURVEY_AUTHENTICATE_KEY'] = md5($string);
    } else {
        $pro = false;
    }
}

Whenever you call Yii::app()->user , you get an instance of CWebUser . 每当调用Yii::app()->user ,都会获得CWebUser的实例。 This is the way Yii represents the user that it currently viewing your application. 这是Yii代表用户当前正在查看您的应用程序的方式。

This user can be logged in or access the app without login (in other words, be a guest). 该用户可以登录或访问应用程序而无需登录(即成为访客)。

Class CWebUser has a method called login , which, as you expected, logs in a user. CWebUser类具有一个名为login的方法,如您所料,它可以登录用户。

Method login() takes as argument an object that implements IUserIdentity interface. 方法login()以实现IUserIdentity接口的对象作为参数。

The easiest way to make your own is to create a simple class (call it MyIdentity for exemple): 制作自己的最简单的方法是创建一个简单的类(例如,将其称为MyIdentity):

//this class' constructor takes a username and a password
class MyIdentity extends CUserIdentity
{
    private $_id;
    public function authenticate()
    {
         // check username and password in DB
         // return true or false to signal whether user should be logged in
    }

    public function getId()
    {
        return $this->_id;
    }
}

Then use what you just created to actually log in an user: 然后使用您刚创建的内容实际登录用户:

// Login a user with the provided username and password.
$identity=new MyIdentity($username,$password);
if($identity->authenticate())
    Yii::app()->user->login($identity);
else
    echo $identity->errorMessage;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM