Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

[SOLVED]User Login With DB

$
0
0
I want to login using the details stored inside a DB.After going through several topics and forums. i edited my Useridentity.php and Login.php files. But nothing is working for me.Can someone pls help.


My useridentity code
public function authenticate()
	{
			$user = TblUser::model()->findByAttributes(array('username'=>$this->username));

		if ($user===null) {
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		} else if ($user->password !== SHA1($this->password) ) { 
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
		} else { // Okay!
		    $this->errorCode=self::ERROR_NONE;
		   
		    
		}
		return !$this->errorCode;
	}



loginform.php

public function rules()
{
	return array(
		array('username, password', 'required'),
		array('password', 'authenticate'),
	);
}

	
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())  // we only want to authenticate when no input errors
		{
			$identity=new UserIdentity($this->username,$this->password);
			$identity->authenticate();
			switch($identity->errorCode)
			{
				case UserIdentity::ERROR_NONE:
					Yii::app()->user->login($identity);
					break;
				case UserIdentity::ERROR_USERNAME_INVALID:
					$this->addError('username','Username is incorrect.');
					break;
				default: // UserIdentity::ERROR_PASSWORD_INVALID
					$this->addError('password','Password is incorrect.');
					break;
			}
		}
	}


sitecontroller.php
public function actionLogin()
	{
		$model=new LoginForm;

		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		$this->render('login',array('model'=>$model));
	}

Viewing all articles
Browse latest Browse all 18717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>