how to redirect to previous page in yii


I am new to Yii. I created a login page and session. It is working fine. My problem is, if I close the window and login again, I want to redirect the previous page before it was closing the window.

Any help is appreciated.

Thanks in advance.


You may want to check out the 2 links below.

Yii::app()->request->urlReferrer will return you the referral URL but as for your case I am not sure whether it will work or not since you mentioned that the browser window was closed and reopened. If i’m not mistaken, session variable will only last until user closes their browser.

Otherwise, you may want to use cookies to keep track of users last visited URL.

Thank You so much.

I will try the above given links.

May I know how to redirect to previous page after logout, and if I login again.

Thank You.

To be able to redirect to the previous URL after logout and relogin, you may place the below code in your controller method.


I have tried this, but not working.

Where should i add the above line of code? in which action?

sorry if the question is so silly. Am new to Yii.

Thank You. :(

Is fine, you should place the code in the action where you validate user access and redirect to the destination URL if conditions are met.