Yii Framework Forum: After Login Successfull Yii::app()->User->Isguest() Always Return True - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

After Login Successfull Yii::app()->User->Isguest() Always Return True Rate Topic: -----

#1 User is offline   ak_yii_learner 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 11-September 13

Posted 08 November 2013 - 02:54 AM

Recently, I have created a new web application by using yiic console command. Unfortunately, I was unable to login to the system with the default login credentials (admin/admin or demo/demo) provided by Yii. There is no error message and it just redirect me to the homepage.

After further checking, I realized that the Yii::app()->user->isGuest() is always return true although I have successfully logged in to the system and the "login" hyperlink on top of the banner wasn't change to "logout".

I didn't modify any code and all the code is originally generated from yiic command.

I'm using Apache/2.2.15, Yii Framework/1.1.14, PHP 5.5.3, Centos 6.4. Will it be any compatibility issues? How can I fixed this issue?


BTW, I'm new to Yii and sorry for my poor English.
0

#2 User is offline   chandran 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 494
  • Joined: 08-October 12
  • Location:Austria

Posted 08 November 2013 - 04:12 AM

Please can you post your code.. it will be helpful to identify what is wrong :(
Thanks
chandran nepolean

My own extension:
http://www.yiiframew...sion/ejqueryte/

My wiki
http://www.yiiframew...checkboxcolumn/

If it work dont hesitate to click +1 button
0

#3 User is offline   ak_yii_learner 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 11-September 13

Posted 08 November 2013 - 04:44 AM

View Postchandran, on 08 November 2013 - 04:12 AM, said:

Please can you post your code.. it will be helpful to identify what is wrong :(


Thanks Chandran. Let me explain the scenario first.

Step 1 - Access to the home page
Attached File  step1.PNG (30.5K)
Number of downloads: 15

Step 2 - Login with invalid login credential, and system manages to prompt me the error message
Attached File  step2.PNG (21.53K)
Number of downloads: 14

Step3 - Login with valid login credential
Attached File  step3.PNG (20.68K)
Number of downloads: 10

Step4 - After login, system redirects to home page but there is no logout and Yii::app()->user->isGuest still return true
Attached File  step4.PNG (30.49K)
Number of downloads: 10

SiteController.php
public function actionLogin()
	{
		$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));
	}


LoginForm.php
<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class LoginForm extends CFormModel
{
	public $username;
	public $password;
	public $rememberMe;

	private $_identity;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// username and password are required
			array('username, password', 'required'),
			// rememberMe needs to be a boolean
			array('rememberMe', 'boolean'),
			// password needs to be authenticated
			array('password', 'authenticate'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'rememberMe'=>'Remember me next time',
		);
	}

	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	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.');
		}
	}

	/**
	 * 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;
	}
}



UserIdentity.php
<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	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;
	}
}



Thanks.
0

#4 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 809
  • Joined: 02-July 10
  • Location:Central Poland

Posted 08 November 2013 - 05:06 AM

if you did not modify anything in code then problem must be in session/cookie handling by your php on server or your browser. You must debug what cookies are set, session id, etc.
red
0

#5 User is offline   chandran 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 494
  • Joined: 08-October 12
  • Location:Austria

Posted 08 November 2013 - 05:09 AM

View Postak_yii_learner, on 08 November 2013 - 04:44 AM, said:

Thanks Chandran. Let me explain the scenario first.

Step 1 - Access to the home page
Attachment step1.PNG

Step 2 - Login with invalid login credential, and system manages to prompt me the error message
Attachment step2.PNG

Step3 - Login with valid login credential
Attachment step3.PNG

Step4 - After login, system redirects to home page but there is no logout and Yii::app()->user->isGuest still return true
Attachment step4.PNG

SiteController.php
public function actionLogin()
	{
		$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));
	}


LoginForm.php
<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class LoginForm extends CFormModel
{
	public $username;
	public $password;
	public $rememberMe;

	private $_identity;

	/**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
	public function rules()
	{
		return array(
			// username and password are required
			array('username, password', 'required'),
			// rememberMe needs to be a boolean
			array('rememberMe', 'boolean'),
			// password needs to be authenticated
			array('password', 'authenticate'),
		);
	}

	/**
     * Declares attribute labels.
     */
	public function attributeLabels()
	{
		return array(
			'rememberMe'=>'Remember me next time',
		);
	}

	/**
     * Authenticates the password.
     * This is the 'authenticate' validator as declared in rules().
     */
	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.');
		}
	}

	/**
     * 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;
	}
}



UserIdentity.php
<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	/**
     * Authenticates a user.
     * The example implementation makes sure if the username and password
     * are both 'demo'.
     * In practical applications, this should be changed to authenticate
     * against some persistent user identity storage (e.g. database).
     * @return boolean whether authentication succeeds.
     */
	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;
	}
}



Thanks.


Hi,

I think you have to change useridentity class like below

I hope it will help you

class UserIdentity extends CUserIdentity
{
private $_id;

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->_id=$this->username;
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId() { return $this->_id; }}

Thanks
chandran nepolean

My own extension:
http://www.yiiframew...sion/ejqueryte/

My wiki
http://www.yiiframew...checkboxcolumn/

If it work dont hesitate to click +1 button
0

#6 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 08 November 2013 - 05:15 AM

Make sure cookies are enabled and php sessions work properly.
0

#7 User is offline   ak_yii_learner 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 11-September 13

Posted 08 November 2013 - 05:41 AM

View Postchandran, on 08 November 2013 - 05:09 AM, said:

Hi,

I think you have to change useridentity class like below

I hope it will help you

class UserIdentity extends CUserIdentity
{
private $_id;

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->_id=$this->username;
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId() { return $this->_id; }}


I have tried this, but still no luck for me. I believe it's something to do with the PHP session or cookies problem like what andy_s and redguy mentioned. Thanks anyway.


View Postandy_s, on 08 November 2013 - 05:15 AM, said:

Make sure cookies are enabled and php sessions work properly.


View Postredguy, on 08 November 2013 - 05:06 AM, said:

if you did not modify anything in code then problem must be in session/ccokie handling by your php on server or your browser. You must debug what cookies are set, session id, etc.


Hi Andy & Redguy, could you guys please guide me on how to check on the session/cookie handling by PHP? Attached herewith the screenshot of the PHP session from php_info.

Thanks

Attached File  phpsession.png (31.44K)
Number of downloads: 10
0

#8 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,694
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 08 November 2013 - 05:59 AM

Two more possible steps:

1. Open your browser's cookie manager and clear all the old cookies from this domain. Then try to login again.

2. (Weird, but cool): What's your domain name look like? I've had this issue on Google Chrome + dev domain w/o TLD (i.e. http://example instead of http://example.local)
God is real unless declared as integer
0

#9 User is offline   ak_yii_learner 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 11-September 13

Posted 08 November 2013 - 09:30 PM

I have fixed the problem and it was due to the permission issues on the PHP session directory.

Thanks everyone.
0

#10 User is offline   Evgeniy Poremchuk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 03-February 13
  • Location:Kyiv, Ukraine

Posted 09 November 2013 - 02:11 AM

Try to store the sessions in database, like:

'session' => array(
'class' => 'CDbHttpSession',
'autoStart' => false,
'connectionID' => 'db',
'sessionTableName' => 'session',
'autoCreateSessionTable' => false,
'cookieMode' => 'none',
),

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users