returnURL still a problem

I am still facing problem in returnURL method as it redirect me to the index page when login page was called directly by clicking login link. I read the whole forum topics regarding this and now i am more confused.

If somebody came up with a solution please help me with that. Thank you.

returnUrl should point to the first url that you want to show after the user logs in

loginUrl will point to the actual login

thank for ur reply but u din get my question. I am asking how to return back to a page after login if the page doesnt cause authentication failure. because


Yii::app()->user->returnUrl

return to index page in this case. only when any page causes authentication failure it return to that page but not directly.

setReturnUrl() will do the trick

you just need to know which page to redirect

setFlash() would do the trick

ok this seems to be good option. I ll try this will inform later.

According to docs, you use setReturnUrl() only to overwrite (change) return page. You don’t have to do this to achieve actual return to the page, where you were before going to login page – this should be don’t automaticaly.

What setFlash() has to do with getting to know, which page to redirect to? I don’t get you.

Dear Friends

The method SiteController::actionLogin calls CWebUser::getReturnUrl method for redirecting the user after successfully logged in.

By going through the code, it is not surprising that if you are not passing a parameter, it is going set the scriptUrl as returnUrl.

CWebUser::getReturnUrl




public function getReturnUrl($defaultUrl=null)

	{

		return $this->getState('__returnUrl', $defaultUrl===null ? Yii::app()->getRequest()->getScriptUrl() : CHtml::normalizeUrl($defaultUrl));

		

	}



So If you want to redirect the user to somewhere else (for example site/contact),you can do it in the following way in SiteController::actionLogin method.




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->getReturnUrl(array('site/contact')));

	}



The setter method stores the returnUrl in session.

CWebUser::setReturnUrl




public function setReturnUrl($value)

	{   

		  $this->setState('__returnUrl',$value);

	}



When we are setting the some other url as returnUrl in main configuration file,the returnUrl still remains the scriptUrl. So it is obvious that returnUrl is not being set, when application sets the user component.

When we are adding the returnUrl as an property in CWebUser, it is being set.

or

When we are modifying the CWebUser::setReturnUrl method to avoid storing the the value in session then it is being set.

or

When I am setting it in a behavior method attached to ‘onBeginRequest’ event

of application, it works very well.

All these things compound my fear that uninitialised status of one component affects the configuration of other

component if it depends on it , when application is configured by settings in main.php .

[color="#8B0000"]Somebody please clarify on this issue.[/color]

Anyway

I modified the controller.php in components folder in the following way.

Now if user clicks login from one page, after successful logging he is the redirected to that particular page.

It is not going to affect the CWebUser::loginRequired.

In order to achieve this, I have consistently storing the last two requests in session.

I DO NOT KNOW whether it is good practice.

CHttpRequest::getUrlReferrer (to catch the ‘HTTP_REFERER’) is always bringing null values in my localhost.

I have also learnt that it is not consistent across browsers. It can also easily inactivated by client side.

Controller.php in Components Folder




class Controller extends CController

{

    public function init()

	{							

		$app=Yii::app();	

		

		$url=$app->request->url;;

		

		if($app->session->contains('pastUrl'))

			$app->user->returnUrl=$app->session->get('pastUrl');		

					

		if($app->session->contains('currentUrl'))

			$app->session->add('pastUrl',$app->session->get('currentUrl'));

							

		$app->session->add('currentUrl',$url);				

	}



Regards.

In protected/component/controller


public function init(){

		Yii::app()->request->cookies['urlReferrerName'] = new CHttpCookie('urlReferrerName', Yii::app()->request->url);//add this line

	}

and in your login action of login controller


//$this->redirect(Yii::app()->user->returnUrl); //comment this line 

   $this->redirect(Yii::app()->request->cookies['urlReferrerName']); //add this line