Yii Error: Cannot modify header information - head

2019-09-02 19:05发布

问题:

This question already has an answer here:

  • How to fix “Headers already sent” error in PHP 11 answers

im a yiibie. what i am trying to do is to get the role of the registered users and show them the content according to their role. i have two users right now

  1. Admin

  2. User(authenticared)

What i am doing is that when my admin logs in via user/login the admin is redirected to the a page which is "webapp/story" and a sidebarwidget is shown to the admin which i have already made and when the user(authenticated) gets logged in via "user/login" that user is just redirected to index.php. after using the code to do this, when my admin gets logged it shows the error:

"Cannot modify header information - headers already sent by (output started at C:\wamp\www\emergency_response\protected\components\views\admin.php:73) "

I have used the code which is given below in and used this code in modules/user/controller/logincontroller.php.

<?php

class LoginController extends Controller
{
public $defaultAction = 'login';

/**
 * Displays the login page
 */
public function actionLogin()
{
    if (Yii::app()->user->isGuest) {
        $model=new UserLogin;
        // collect user input data
        if(isset($_POST['UserLogin']))
        {
            $model->attributes=$_POST['UserLogin'];
            // validate user input and redirect to previous page if valid
            if($model->validate()) {
                $this->lastViset();
  //this is the code which i have used for the task i am doing.
                $role = Rights::getAssignedRoles(Yii::app() -> user     ->     Id);
          foreach ($role as $role)
            $role -> name;
        if ($role -> name == 'Admin'){
            $this->widget('AdminWidget') &&     $this->redirect(array('story'));
        }
        else{
            $this->redirect(Yii::app()->user->returnUrl);
        }
            }
        }
        // display the login form
        $this->render('/user/login',array('model'=>$model));
    } else
        $this->redirect(Yii::app()->controller->module->returnUrl);
    }

private function lastViset() {
    $lastVisit =     User::model()->notsafe()->findByPk(Yii::app()->user->id);
    $lastVisit->lastvisit = time();
    $lastVisit->save();
    }

}

回答1:

You can try the easy one first:

Replace:

$this->widget('AdminWidget') && $this->redirect(array('story'));

To:

$this->widget('AdminWidget');
$this->redirect(array('story'));

But i don't think that the above will work because you will have a redirect and the first statement of $this->widget('AdminWidget'); will not take effect after the redirect.

Second thought is to make some changes because i think you had logic errors. Try this:

class LoginController extends Controller {
    public $defaultAction = 'login';

    /**
     * Displays the login page
     */
    public function actionLogin() {
        if (Yii::app()->user->isGuest) {
            $model = new UserLogin;
            // collect user input data
            if (isset($_POST['UserLogin'])) {
                $model->attributes=$_POST['UserLogin'];
                // validate user input and redirect to previous page if valid
                if ($model->validate()) {
                    $this->lastViset();
                    //this is the code which i have used for the task i am doing.
                    $role = Rights::getAssignedRoles(Yii::app()->user->Id);
                    foreach($role as $role) {   //Loop every role
                        if ($role->name == 'Admin'){    //If a role is Admin then redirect
                            $this->redirect(array('webapp/story'));
                        }
                    }
                    //If none of the roles were admin then redirect to a user view
                    $this->redirect(Yii::app()->user->returnUrl);
                }
            }
            // display the login form
            $this->render('/user/login', array('model'=>$model));
        } else {
            $this->redirect(Yii::app()->controller->module->returnUrl);
        }
    }

    private function lastViset() {
        $lastVisit =     User::model()->notsafe()->findByPk(Yii::app()->user->id);
        $lastVisit->lastvisit = time();
        $lastVisit->save();
    }

}

In WebappController:

class WebappController extends Controller {

    public function actionStory() {
        $this->widget('AdminWidget');   //Maby you have to add this inside the view
        //Other code
        $this->render('webapp');
    }
}


标签: php yii