Login Page not Working

view file login.php


<?php 

	$form=$this->beginWidget('CActiveForm', array(

		'id'=>'userregistration-login-form',

		'enableAjaxValidation'=>true,

		'enableClientValidation'=>true,

		'clientOptions'=>array('validateOnSubmit'=>true),		

	));   ?>

<div id="form1">

                                <div id="formDiv1">

	<p style="margin: 0 0 20px 85px" class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo CHtml::$afterRequiredLabel = ' <span class="pink-color">*</span>'; echo $form->errorSummary($model); ?>


	<div class="frmFields" style="margin: 0 0 0 40px;">

		<?php echo $form->labelEx($model,'email', array("style"=>"color:#C29A4B; font-weight:bold")); ?>

		<?php echo $form->emailField($model,'email', array("style"=>"border:1px solid #DCDCDD")); ?>

		<?php echo $form->error($model,'email'); ?>

	</div>


	<div class="frmFields" style="margin: 0 0 0 40px;">

		<?php echo $form->labelEx($model,'password', array("style"=>"color:#C29A4B; font-weight:bold")); ?>

		<?php echo $form->passwordField($model,'password', array("style"=>"border:1px solid #DCDCDD")); ?>

		<?php echo $form->error($model,'password'); ?>

	</div>




	<div id="frmBtnLink1">

		<div style="float:left;">

			<?php echo CHtml::submitButton('', array("id"=>"Login", "name"=>"Login", "style"=>"border-width:0px;")); ?>

		</div>

		<div style="float:left; margin-left:7px; padding-top:7px;"><a href="<?php echo Yii::app()->createUrl('site/registration'); ?>" style="color:#000000; text-decoration:underline; font-weight:bold;">Need an Account</a></div>

        <div style="float:left; margin-left:7px; padding-top:7px;"><a href="javascript:forgotPassword();" style="color:#C29A4B; font-weight:bold; text-decoration:underline;" id="forgot">Forgot your password?</a></div>

	</div>

	

	<?php echo $form->hiddenField($model,'admin', array('value'=>'0')); ?>

</div>

</div>




<?php $this->endWidget(); ?>

model LoginForm.php


class LoginForm extends CFormModel

{

	public $email;

	public $password;


	private $_identity;


	public function rules()

	{

		return array(


			array('email, password', 'required'),


			array('email', 'email'),


			array('password', 'authenticate'),

		);

	}


	/**

	 * Declares attribute labels.

	 */

	public function attributeLabels()

	{

		return array('email'=>'User Name/Email Address'


		);

	}


	public function authenticate($attribute,$params)

	{

		if(!$this->hasErrors())

		{

			$identity=new UserIdentity($this->email,$this->password);

			$identity->authenticate();

			switch($identity->errorCode)

			{

				case UserIdentity::ERROR_NONE:

					Yii::app()->user->login($identity);

					break;

				case UserIdentity::ERROR_USERNAME_INVALID:

					$this->addError('email','Email address is incorrect.');

					break;

				default: // UserIdentity::ERROR_PASSWORD_INVALID

					$this->addError('password','Password is incorrect.');

					break;

			}

		}

	}


	public function login()

	{

		if($this->_identity===null)

		{

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

			$this->_identity->authenticate();

		}

		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)

		{

			Yii::app()->user->login($this->_identity,0);

			return true;

		}

		else

			return false;

	}

}



SiteController login action


public function actionLogin()

	{

	

	   if(isset($_SESSION["Search"]) && $_SESSION["Search"]!= null){

		  $this->redirect(array($_SESSION["Search"]));	

	   }

	

	

		$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));

	}

Whenever I fill the email and password fields and submit I get this error: User Name/Email Address cannot be blank.

Hi!

Since your are using ajax validation, you need to set the same id on widget(CActiveForm) and Ajax Validation. In this case, ‘id’=>‘login-form’, i.e.




...

<?php 

        $form=$this->beginWidget('CActiveForm', array(

                'id'=>'login-form',

                'enableAjaxValidation'=>true,

                'enableClientValidation'=>true,

                'clientOptions'=>array('validateOnSubmit'=>true),               

        ));   ?>

...






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();

                }

...  

}              



I found the reason. The forgot your password div was the reason because it’s fields were not being filled when submitting form. the forgot div is hidden when the page loads and shows up when the user clicks on forgot your password link


<div id="form2" style="display:none;">

<div id="formDiv2">

	<p style="margin: 0 0 20px 85px" class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo CHtml::$afterRequiredLabel = ' <span class="pink-color">*</span>'; echo $form->errorSummary($model); ?>


	<div class="frmFields" style="margin: 0 0 0 40px;">

		<?php echo $form->labelEx($model,'email', array("style"=>"color:#C29A4B; font-weight:bold")); ?>

		<?php echo $form->emailField($model,'email', array("style"=>"border:1px solid #DCDCDD")); ?>

		<?php echo $form->error($model,'email'); ?>

	</div>


	<div id="frmBtnLink2">

		<div style="float:left;">

			<?php echo CHtml::submitButton('', array("id"=>"Send", "name"=>"Login", "style"=>"border-width:0px;")); ?>

		</div>

	</div>

	

	<?php echo $form->hiddenField($model,'admin', array('value'=>'0')); ?>

</div>




<script type="text/javascript" language="javascript">

        function forgotPassword() {

            jQuery("#form1").hide();

            jQuery("#form2").show();

        }

    </script>


    <script type="text/javascript" language="javascript">

        jQuery(document).ready(function () {

            jQuery("#form2").hide();

            jQuery("#form1").show();

        });

    </script>

</div>

So what’s the solution to this problem ? the page gives error message username/email is blank if I submit the form and if the form2 div is there

print this in login action

print_r($_POST[‘LoginForm’]);

exit;

in localhost login page is working correctly but in live site I get password is not correct. table in database has this entry then why is it showing password is not correct on live site ? print_r($_POST[‘LoginForm’] gives this output.


Array ( [email] => aaa@aaa.com [password] => aaa [admin] => 0 ) 

Are you sure that the password is incorrect, because you give the "wrong password" message as default message in your LoginForm.php authenticate() method.

Try to uncomment this line and set another default message and see if the wrong password message still appears.

the password is correct. in localhost it’s not giving any error and redirecting to index page but live site is giving ‘password is incorrect’ message

Can you maybe post you UserIdentity class? Because that’s the place where this error is generated.

And do you maybe use something like encrypting of your passwords?


class UserIdentity extends CUserIdentity

{

	 private $_id;

	 

	public function authenticate()

	{

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

		

		if($user===null)

			$this->errorCode=self::ERROR_USERNAME_INVALID;

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

			$this->errorCode=self::ERROR_PASSWORD_INVALID;

		else{

			$this->errorCode=self::ERROR_NONE;

			$this->_id = $user->id;

			$this->setState('role', $user->admin);

			}

		return !$this->errorCode;

	}

	public function getId()

	{

		return $this->_id;

	}

}

What’s the var_dump in your UserIdentity class of:


$user->password

and


$this->password

Using print_r($_POST[‘LoginForm’]); in controller

and

var_dump ($this->password);

var_dump ($user->password); in UserIdentity class gives


Array ( [email] => abc@abc.com [password] => abc [admin] => 0 ) string(3) "abc" string(3) "abc"