I want to redirect to log in page if session is null at application level using Yii framework. It should be applicable to all pages except log in page. Any helps appreciated
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The best way to do it would be to modify the Controller base class (protected/components/Controller.php). You can add an
init()function which will get executed on every Controller call, something like:You can access the current Controller with:
But you cannot access the Action in the init function (usually you could use
Yii::app()->controller->action->id). The easiest way to do this is probably to check the URL like:You might have to edit the
$_SERVER['REQUEST_URI']before comparing if you are working on your localhost in a sub folder, something like:And then compare with that. Also probably best to check for the index.php in the string as well.
I am assuming you are using this for making certain pages not accesible to non logged in users? If that is the case you really should be using
accessRules. Read more about that here