Get The Url Of The Page Coming From

In search page I setup a session variable when the page loads. If the user clicks on a save link without being logged in he is redirected to the login page. After successful login he is to be redirected to the same search page. But if the user loads the search page, the session variable is set. If he then goes to index page then goes to login page I don’t want to redirect to search page.

Here is what I have implemented in login action


if(Yii::app()->getController()->getAction()->id=="search"){

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

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

		   }

	   }

	   else{

		unset(Yii::app()->session['Search']);

	   }

This is the session variable that is set when the search page loads




$searchurl = end(split('/', $_SERVER['REQUEST_URI']));

Yii::app()->session['Search'] =  $searchurl;


if(Yii::app()->getController()->getAction()->id=="search"){...}

What is this doing? Surely if this is implemented in the login action, the action ID is always going to be "login"?

what u mean?




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



This is what I was looking for Yii::app()->user->returnUrl. But how can I check the controller or action id of returnUrl ?

Thanks Rajith. How can I check the controller or action id of returnUrl ?

You don’t need it. Just use CController::redirect and you’re set.

But on successful login on the login page I want to redirect to search page only if the user has come from the search page whose url is …index.php/site/serach?.. In other cases I want to redirect to a certain page after login, which I have already done.

I’m not sure if it’s good advice, but you could probably look for the substring “site/search” in the URL and base your decision on that. It doesn’t sound like a clean or robust solution though.

Understood.

In this case, you can use CWebUser::returnUrl only for those special cases (like when the user comes from the search page).

When returnUrl is not set, you redirect the user to the default page after login.

set in config/main


defaultController'=>'controller/action',

this will full fill ur second demand…

and for search check if(Yii::app()->user->returnUrl ==‘index.php/site/serach’) then return to search …simple…

Using




die(Yii::app()->user->returnUrl); if(Yii::app()->user->returnUrl =='index.php/site/serach'){

is giving /Index.php

In config/main rules array when I used ‘site’=>‘controller/action’, die(Yii::app()->user->returnUrl); gave the same output /Index.php

‘defaultController’=>‘controller/action’, also gave the same output

Please follow Rajith’s recommendation and set the defaultController.

Then you can just redirect to the returnUrl after login, it will always point to the returnUrl you set before the login or to the defaultController.