简体   繁体   中英

Yii successful login redirects to a wrong index.php not the one I intend to

I am creating an application based on Yii framework, now I'm trying to setup the login for it, as I have shown in the picture below. The Yii folder is within my web application directory. In the login of web application when I call

Login_yii/index.php?r=site/login

as the action of my login form (one of the scripts I have circled) it redirects to the index.php of the yii folder instead of redirecting to my index.php(which i have circled in the picture). Can anybody help me on this? I even tried modifying the site controller

if($model->validate() && $model->login())
    $this->redirect(array("/<controller>/<action>"));

but it didn't work but not sure whether I gave the correct path to it, and not sure what shall I put for the controller to get my own index

File structure:

在此输入图像描述

login.php:

<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
{
    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',
            ),
        );
    }


    public function actionIndex()
    {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->render('index');
    }


    public function actionError()
    {
        if($error=Yii::app()->errorHandler->error)
        {
            if(Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }


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

        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);
                  $this->redirect(array("/<controller>/<action>"));
        }
        // display the login form
        $this->render('login',array('model'=>$model));*/
    }


    public function actionLogout()
    {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }
}

Try like this -

if (isset($_POST['LoginForm'])){
    $model->attributes = $_POST['LoginForm'];

    if ($model->validate(array('username', 'password')) && $model->login()) //if authentication successfull
    $this->redirect(array('site/dashboard'));
}

Try using the base url:

$this->redirect( Yii::app()->getBaseUrl(true) . '/site/index' );

getBaseUrl returns the relative/absolute URL for the application. From there, you have to specify which path you want to run.

If you want to call files in a parent directory, you will have to use '..' :

$this->redirect( Yii::app()->getBaseUrl(true) . '/../site/index' );

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