简体   繁体   中英

Yii login does not redirect to the page i want

I configured an Yii demo login application, by using its scripts and functions i tried to use it as the back end for the login page of my own website. which is html and its on a seperate directory.

It does get authenticated correctly but instead of redirecting to where i want after a successful login it goes to its own index of yii demo application. can anybody help on this to redirect to my own page.

<div class="col-xs-12 col-sm-12 col-md-5 col-lg-4">
        <div class="well no-padding">
            <form action="Login_yii/index.php?r=site/login" id="login-form" class="smart-form client-form">
                <header>
                    Sign In
                </header>

                <fieldset>

                    <section>
                        <label class="label">Username</label>
                        <label class="input"> <i class="icon-append fa fa-user"></i>
                            <input type="text" name="LoginForm[username]" id="username">
                            <b class="tooltip tooltip-top-right"><i class="fa fa-user txt-color-teal"></i> Please enter the username</b></label>
                    </section>

                    <section>
                        <label class="label">Password</label>
                        <label class="input"> <i class="icon-append fa fa-lock"></i>
                            <input type="password" name="LoginForm[password]" id="password">
                            <b class="tooltip tooltip-top-right"><i class="fa fa-lock txt-color-teal"></i> Enter your password</b> </label>
                        <div class="note">
                            <a href="<?php echo APP_URL; ?>/forgotpassword.php">Forgot password?</a>
                        </div>
                    </section>

                    <section>
                        <label class="checkbox">
                            <input type="checkbox" name="remember" checked="">
                            <i></i>Stay signed in</label>
                    </section>
                </fieldset>
                <footer>
                    <button type="submit" class="btn btn-primary" su>
                        Sign in
                    </button>
                </footer>
            </form>

Controller

    <?php

class SiteController extends Controller
{
/**
 * Declares class-based actions.
 */
public function actions()
{
    return array(
        // captcha action renders the CAPTCHA image displayed on the contact page
        'captcha'=>array(
            'class'=>'CCaptchaAction',
            'backColor'=>0xFFFFFF,
        ),
        // page action renders "static" pages stored under 'protected/views/site/pages'
        // They can be accessed via: index.php?r=site/page&view=FileName
        'page'=>array(
            'class'=>'CViewAction',
        ),
    );
}

/**
 * This is the default 'index' action that is invoked
 * when an action is not explicitly requested by users.
 */
public function actionIndex()
{
    // renders the view file 'protected/views/site/index.php'
    // using the default layout 'protected/views/layouts/main.php'
    $this->render('index');
}

/**
 * This is the action to handle external exceptions.
 */
public function actionError()
{
    if($error=Yii::app()->errorHandler->error)
    {
        if(Yii::app()->request->isAjaxRequest)
            echo $error['message'];
        else
            $this->render('error', $error);
    }
}

/**
 * Displays the contact page
 */
public function actionContact()
{
    $model=new ContactForm;
    if(isset($_POST['ContactForm']))
    {
        $model->attributes=$_POST['ContactForm'];
        if($model->validate())
        {
            $name='=?UTF-8?B?'.base64_encode($model->name).'?=';
            $subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
            $headers="From: $name <{$model->email}>\r\n".
                "Reply-To: {$model->email}\r\n".
                "MIME-Version: 1.0\r\n".
                "Content-Type: text/plain; charset=UTF-8";

            mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
            Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
            $this->refresh();
        }
    }
    $this->render('contact',array('model'=>$model));
}

/**
 * Displays the login page
 */
public function actionLogin()
{
        $form=new LoginForm;
        // collect user input data
        if(isset($_POST['LoginForm']))
        {
            $form->attributes=$_POST['LoginForm'];
            // validate user input and redirect to previous page if valid
            if($form->validate()  && $form->login()) $this->redirect(Yii::app()->user->returnUrl);

        }
            // display the login form
            $this->render('login',array('form'=>$form));

    /*$model=new LoginForm;

    // if it is ajax validation request
    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }

    // collect user input data
    if(isset($_POST['LoginForm']))
    {
        $model->attributes=$_POST['LoginForm'];
        // validate user input and redirect to the previous page if valid
        if($model->validate() && $model->login())
            $this->redirect(Yii::app()->user->returnUrl);
    }
    // display the login form
    $this->render('login',array('model'=>$model));*/
}

/**
 * Logs out the current user and redirect to homepage.
 */
public function actionLogout()
{
    Yii::app()->user->logout();
    $this->redirect(Yii::app()->homeUrl);
}

}

file structure

在此处输入图片说明

index of my web and yii folder are highlighted

In your site controller's login action just change the redirect from $this->redirect(Yii::app()->user->returnUrl); to $this->redirect(array("/<controller>/<action>"));

    public function actionLogin()
    {
        $model=new LoginForm;

        // if it is ajax validation request
        if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if(isset($_POST['LoginForm']))
        {
            $model->attributes=$_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if($model->validate() && $model->login())
                $this->redirect(array("/<controller>/<action>"));//change the controller and action you want to redirect to.
        }
        // display the login form
        $this->render('login',array('model'=>$model));
    }

Ok You are redirecting to previous url (just before login) with if($form->validate() && $form->login()) $this->redirect(Yii::app()->user->returnUrl);

Redirect to Your URL of choice using CController::redirect()

See another question Yii not redirecting properly with the CController::redirect() function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM