繁体   English   中英

Yii-如何防止用户登录后访问登录页面

[英]Yii - How to Prevent User Accessing Login Page After Login

大家。 我想防止用户登录后访问登录页面。最好的方法是什么? 这是config / main / php的代码

return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My Web',
'defaultController' => 'site/login')

和SiteContoller.php

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()){
            $isAdmin = Yii::app()->user->getLevel() <=1; // or how you define admin in your case.
            if ($isAdmin)
                $this->redirect(array('user/view', 'id'=>Yii::app()->user->id));
            else
            //$id = Yii::app()->user->id;
                $this->redirect(array('site/index'));
        }
    }
    // display the login form
    $this->render('login',array('model'=>$model));
}

'defaultController'=>'site/login'我还希望用户在关闭浏览器并重新打开后不访问登录页面。 那可能吗? 非常感谢

您可以尝试这种简单的方法。

public function actionLogin()
{

   if(!Yii::app()->user->isGuest)
   {
       $this->redirect(array('index')); 
   }

   // rest of the codebelow.

}

您可以定义访问规则,并添加不同类型的用户访问的url。 例如:

class YourController extends Controller {

public function filters() {
    return array(
        'accessControl', // perform access control for CRUD operations
    );
}

/**
 * Specifies the access control rules.
 * This method is used by the 'accessControl' filter.
 * @return array access control rules
 */
public function accessRules() {
    return array(
        array('allow', // allow all users to perform 'index' and 'view' actions
            'actions' => array('index', 'view'),
            'users' => array('*'),
        ),
        array('allow', // allow Only logged in user's to delete and update
            'actions' => array('delete', 'update'),
            'users' => array('*'),
        ),
        array('deny', // deny all users
            'users' => array('*'),
        ),
    );
}

这是在Yii中使用的最佳,简便和适当的方法。

使用Sessions可以做到这一点。 您需要做的是在代码顶部运行的启动会话。 如果您曾提及是否登录过用户,则可能需要在代码的其他部分中包含此内容。

$sid = $session_id();
if(isset($sid)){
    //session exists
} else {
    start_session();
}

之后,您将要添加一个会话条目以跟踪您是否有登录用户。 我建议您在确认用户这样登录后执行此操作:

// 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()){
            // store that the user is logged in
            $this->session->set_userdata(array('loggedIn' => true));
            $isAdmin = Yii::app()->user->getLevel() <=1; // or how you define admin in your case.
            if ($isAdmin)
                $this->redirect(array('user/view', 'id'=>Yii::app()->user->id));
            else
            //$id = Yii::app()->user->id;
                $this->redirect(array('site/index'));
        }
    }

最后,您可以在进入表单处理程序之前检查代码,以查看是否有人使用会话变量登录。 如果是这样,则可以将其发送到站点/索引或用户/视图,否则可以照原样继续。

if(isset($this->session->userdata('loggedIn')){
    //user is logged in, redirect away from this page
}
// else the user is not logged in and continue as normal

理想情况下,您可以使用上述三行代码以及session_start()代码来检查用户是否在任何页面上登录,这通常是实现私有/公共页面以及让人们关闭浏览器/页面的好方法。然后返回而没有注销。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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