2014-03-28 172 views
0

everyone。 我想阻止用户在登录后访问登录页面。 这样做的最佳方式是什么? 这里的config /主/ PHPYii - 如何防止用户在登录后登录登录页面

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'我也希望用户没有访问登录页面,他们关闭布劳尔并重新打开它后。那可能吗?非常感谢

回答

2

你可以试试这种方式,它很简单。

public function actionLogin() 
{ 

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

    // rest of the codebelow. 

} 
0

这可以使用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()代码来检查,如果用户登录任何网页,它通常是实施私有/公共页面的好​​方法上,以及让人们关闭浏览器/页面,然后返回而不被注销。

1

您可以定义访问规则并添加不同类型用户访问的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中使用..

相关问题