drm16022 2014-09-29 06:41
浏览 74

Yii登录不会重定向到我想要的页面

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}>
".
                "Reply-To: {$model->email}
".
                "MIME-Version: 1.0
".
                "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

enter image description here

index of my web and yii folder are highlighted

  • 写回答

2条回答 默认 最新

  • donglu8344812 2014-09-29 07:27
    关注

    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));
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?