Page redirection issue in Yii

2019-09-01 18:17发布

问题:

i have a problem in my application which is built in Yii... when i want to visit a link that show me an error in FireFox as..

The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

and in chrome it says...

This webpage has a redirect loop

one thing more it is working fine on local machine; no issues but the above error is on live server...

Now what is the main issue here..?

回答1:

It seems that the action 'login' is not allowed to be viewed by any users. Yii's default behavior when a user tries to access a member-only page is to redirect it to the login page, and since your login action is not allowed to guest users, Yii is redirecting again to the login page thus resulting in an endless loop and too-many-redirect error

Check your accessRules() function on the controller class, "UserController.php" in your case. You need to let "Guest" user or ANY user to perform the action, "actionLogin()" in this case.

just add 'login' to "actions array" and check "users array" value is '*'

// FILE = WebRoot/protected/controllers/UserController.php

//...
public function accessRules()
        {
                return array(
                        array('allow',  // allow all users to perform 'index' and 'view' actions
                                'actions'=>array('index','view','login'),
                                'users'=>array('*'),
        //...
        }
//...   

you may have this problem when you delete the default login and then try to create your own,this may solve the problem.



回答2:

After few days fighting with loop redirect it was solved in non sexy way, but it works

if (stristr($_SERVER['HTTP_USER_AGENT'], 'Firefox') ) echo '<meta http-equiv="refresh" content="0; url='.Yii::app()->createUrl('url/goes/there').'">' ; 
else $this->redirect(Yii::app()->createUrl('url/goes/there'));


标签: php yii