• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于PHP的Yii框架中登陆功能的实现

php 搞代码 3年前 (2022-01-22) 32次浏览 已收录 0个评论

这篇文章主要介绍了详细解读PHP的Yii框架中登陆功能的实现,包括通过cookie实现自动登录的功能,需要的朋友可以参考下

Yii的登陆机制

Yii 生成应用时已经提供了最基础的用户登陆机制。我们用 Yii 生成一个新的应用,进入 protected/components 目录,我们可以看到 UserIdentity.php 文件,里面的 UserIdentity 类里面只有一个 public 函数如下:

public function authenticate() {   $users=array(     // username => password     'demo'=>'demo',     'admin'=>'admin',   );   if(!isset($users[$this->username]))     $this->errorCode=self::ERROR_USERNAME_INVALID;   elseif($users[$this->username]!==$this->password)     $this->errorCode=self::ERROR_PASSWORD_INVALID;   else     $this->errorCode=self::ERROR_NONE;   return !$this->errorCode; }

这个类在 components 里面,会在应用一开始的时候就加载,用于最基础的用户验证,可以看到,该函数一开始只是简单地定义了两个用户 demo 和 admin,而密码也只是 demo 和 admin,如果所以如果你的用户很有限的话,可以直接在这里面修改添加用户就行,多的话我们后面再说。函数下面的 if else 分别是用于检查用户名和密码是否有效,出错的时候生成 ERROR_USERNAME_INVALID,ERROR_PASSWORD_INVALID 这些错误。总的来说,这里进行了真正的用户名密码验证,并进行登陆后的基本逻辑处理。
单看这个类还是看不出登陆控制流程的。遵循 Model/ Con、本文来源gao($daima.com搞@代@#码$网搞gaodaima代码trol/ View 的原则,我们可以看到登陆流程在这三方面的体现。首先进入 Models 文件夹,你可以看到一个 LoginForm 的类文件,这个类继承了 CFormModel ,为表单模型的派生类,封装了关于登陆的数据及业务逻辑。比较核心的函数如下:

/**  * Authenticates the password.  * This is the 'authenticate' validator as declared in rules().  */ public function authenticate($attribute,$params) {   $this->_identity=new UserIdentity($this->username,$this->password);   if(!$this->_identity->authenticate())     $this->addError('password','用户名或密码错误'); }  /**  * Logs in the user using the given username and password in the model.  * @return boolean whether login is successful  */ public function login() {   if($this->_identity===null)   {     $this->_identity=new UserIdentity($this->username,$this->password);     $this->_identity->authenticate();   }   if($this->_identity->errorCode===UserIdentity::ERROR_NONE)   {     $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days     Yii::app()->user->login($this->_identity,$duration);     return true;   }   else     return false; }

这里的 authenticate 利用 UserIdentity 类对用户名密码进行验证,而 login 函数通过检测用户身份是否已经设置及错误码是否为空,最后进行 Yii 提供的 login 函数进行登陆。$duration 可以设置身份的有效期。
再看 Control,在 siteControler 里面有一个 action 是关于登录的,就是 actionLogin, 函数如下:

/**  * Displays the login page  */ public function actionLogin() {   if (!defined('CRYPT_BLOWFISH')||!CRYPT_BLOWFISH)     throw new CHttpException(500,"This application requires that PHP was compiled with Blowfish support for crypt().");    $model=new LoginForm;    // if it is ajax validation request   if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')   {     echo CActiveForm::validate($model);     Yii::app()->end();   }    // collect user input data   if(isset($_POST['LoginForm']))   {     $model->attributes=$_POST['LoginForm'];     // validate user input and redirect to the previous page if valid     if($model->validate() && $model->login())       $this->redirect(Yii::app()->user->returnUrl);   }   // display the login form   $this->render('login',array('model'=>$model)); }

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于PHP的Yii框架中登陆功能的实现
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址