Returnurl Always Points To Index.php

I have exactly the same problem as GSTAR had, but in my case Yii::app()->user->returnUrl is always pointing to index.php in my application.

And thus, login always redirects me to home page, not there, where I was.

I printed it out to myself in main.php layout file, so I see it on very page. No matter, where I go, it is always index.php, not route to the previously visited page (as supposed to be).

Anyone have any idea, what can be wrong?

I have deployed a fresh, auto-generated Yii application. I have changed it strongly ever since, but I did not mess up anything around redirects, referrers, login etc.

Cookies for sure works, because I can mark “remember me” in login page and don’t have to login again, after restarting browser.

I don’t know what the problem may be. I have tried returnUrl then ended up using urlReferrer. Don’t know if it’s flawless.

See also this thread. Interesting ideas!

http://www.yiiframework.com/forum/index.php/topic/11959-any-yii-way-to-get-the-previous-url/

The topic, you cited is over two years old and shows way to solve the problem that is already solved in newest versions of Yii – currently CWebUser is setting returnUrl and nothing of what is shown in that old topic should be done manually.

I started the topic to look for any ideas, why my CWebUser is not setting this variable properly.

Dear Friend

This is something to share with you on this topic.

CWebUser has no property declared as returnUrl.

When we are going to set this property in main configuration file,it is going to invoke the

method CWebUser::setReturnUrl.




public function setReturnUrl($value)

	{   

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

	}



This method instead of assigning the value to returnUrl, it is storing the value in session.

My fear :

When application configures the components by reading the main configuration file, Does the uninitialised state of other components or a component in question affect the creation of that particular component?.In our case when the component user is crearted, is the component session available to configure the user?.

situation 1; This works…

main.php




'components'=>array(

		'user'=>array(

		    'class'=>'application.components.webUser',

		    'allowAutoLogin'=>true,

		     'returnUrl'=>'index.php?r=site/contact',

		),



webUser.php in components folder.




<?php

class webUser extends CWebUser

{

	public $returnUrl;

}	



situation 2: This works… without the property returnUrl




'components'=>array(

		'user'=>array(

		    'class'=>'application.components.webUser',

		    'allowAutoLogin'=>true,

		     'returnUrl'=>'index.php?r=site/contact',

		),



webUser.php in components folder.




class webUser extends CWebUser

{

	//public $returnUrl;	

	public function getReturnUrl($defaultUrl=null)

		{

			return $this->returnUrl;

		}


       public function setReturnUrl($url)

		{

			$this->returnUrl=$url;

		}

}

  

	



situation 3: It works…

Instead of declaring the defaultUrl in main.php, if we declare it down the lane, it works.

Here I am declaring it in a behavior attached to main application without extending the CWebUser.

Main.php




..............................

'behaviors'=>array(

'class'=>'application.components.applicationBehavior',

),

..............................

'components'=>array

		'user'=>array(

			'allowAutoLogin'=>true,

		),



applicationBehavior in components folder.




<?php

class applicationBehavior extends CBehavior

{       

      private $_owner;

      public function events()

           {


                return  array(

			       

			       'onBeginRequest'=>'assignReturnUrl',

			        

		        );

           }

	

		

		public function assignReturnUrl()

               {

			$owner=$this->_owner=$this->getOwner();

			$owner->user->returnUrl='index.php/?r=site/contact';

		}

	

}

?>




All these results compound my fear that the problem arises when components are created and initialized.

I need your inputs on this.

As suggested by bennouna, I was trying to get ‘HTTP_REFERER’ from CHttpRequest::getUrlReferrer to get

returnUrl.It is always bringing null in my localhost in all the browsers.

I tried to catch the the last two requests and put them in session. So when user jumps from one particular page into login page, after successful login he again redireced to that particular page.

applicationBehavior.php in components folder.




<?php

class applicationBehavior extends CBehavior

{       

      private $_owner;

      public function events()

           {


                return  array(

			       

			       'onBeginRequest'=>'assignReturnUrl',

			        

		        );

           }

	

		

		public function assignReturnUrl()

               {

			$owner=$this->_owner=$this->getOwner();


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

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

			

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

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


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

		}

	

}

?>




The issue needs further clarification.

Regards.

Hi,

Thank you for your long and detailed answer.

Hm… What about this?

There is such property like CWebUser.returnUrl, but – as I discovered – it is only set properly in situations, where login process is called automatically by Yii. That is: if user is not logged-in (authenticated) and requests any operation that requires authentication. In any other situation, returnUrl contains "index.php", and this is considered as Yii feature, not a bug (at least now).

This is kind of big problem. You not only have to record your current URL, but you also have to take care of not recording login form URL or actions like CCaptchaActions route. In other word, this is (as for me) to big task, for time resources I have to spend on this. So, unless this will become fixed in next release of Yii, I’m leaving it like it is now – i.e. after manual login, user will be redirected to home page.

Hi all

I am experiencing a similar issue. I am new to Yii and have read some of the posts and have managed to get some aspects working, such as after successful login, going to the correct page and not the index page as before.

However, when I enter this web address ‘http://trackstar:8888/index.php?r=project’ in the browser I am not redirected to the Login screen as I should because I haven’t logged in yet.

I am working from Jeffrey Winesett’s latest book (2012) on Yii and he states that after running the CRUD Generator, and without any configuration of the application it should work: ie

#1 Successfully log in and you should go to the ‘project’ section of the app.

#2 enter the above address and you should be redirected to the Login screen.

Anyone wanting to know what I have done to fix the first part see below. As far as the second issue is concerned I am at a loss.

(from SiteController)


	public function actionLogin()

	{

		$model=new LoginForm;

		$yourUrl='index.php?r=project/create';   //add this

		


		// 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);  // Orignal code


				$this->redirect(Yii::app()->user->getReturnUrl($yourUrl)); // and Add this!!!


		}

		// display the login form

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

	}