Login With Db Failed

Halo, I have tried to do a login activity in Yii with database. I think I didn’t make mistake in my program, but when I input the username and password, the application does nothing but redirect to the Homepage without I logged in. I have tried to override the $_id, but still same. I hope anyone can teach me how can I fix it. Thanks all.

So, here’s my code:

  1. The UserIdentity.php

class UserIdentity extends CUserIdentity

{

   private $_id;

   public function authenticate()

    {

        $user = User::model()->findByAttributes(array('username'=>$this->username));


			if ($user===null) {

                $this->errorCode=self::ERROR_USERNAME_INVALID;

            }

			else if ($user->password !== md5($this->password) ) { 

                        $this->errorCode=self::ERROR_PASSWORD_INVALID;

            }

			else { // Okay!

                    $this->errorCode=self::ERROR_NONE;

                   

                    

                }

                return !$this->errorCode;

	}

	

	public function getId()       //  override Id

	{

       return $this->_id;

	}

}

  1. 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'];

                        

                        // validate user input and redirect to the previous page if valid

                        if($model->validate() && $model->login())

                        {

                          $this->redirect(array("index"));

                        

                        }

                                

                }

                        $this->render('login',array('model'=>$model));

	}

}

  1. authenticate function in LoginForm.php

public function authenticate($attribute,$params)

	{

		if(!$this->hasErrors())

                {

                        $this->_identity=new UserIdentity($this->username,$this->password);

                        if(!$this->_identity->authenticate())

                                $this->addError('password','Incorrect username or password.');

                }

	}

If it redirects to the home page, it suggests that authentication has been successful, but the session hasn’t been correctly created or the session cookie hasn’t been set.

See if the advice on this page helps.

Hi, Keith.

Firstly, thanks for the reply.

Okay but I don’t know how to configure it. I am pretty new in PHP and Yii. Can you show me how to configure it?

Thanks a lot

Can you post the content of the LoginForm::login() method?

Sure, will do:


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;

	}

That looks like it should work to me. Use your browser’s cookie viewer to see whether a cookie has been set for the domain (or IP) that you’re testing on. I think the cookie is called PHPSESSID by default.

I have seen the cookie and there is a cookie called PHPSESSID, but why still not working for me? I use Google Chrome as my browser

Here’s I send you the screenshot of that:

4914

cookie.png

Try clearing your local cookies and login again.

Still not working for me, I also tried in Mozilla and the result just the same. Seems hopeless

It must be the session configuration then. Did you try copying the config from the page I linked earlier?

Okay, I haven’t tried it. So, where can I put that config? I don’t know where