doulangtiao4508 2014-11-27 19:32
浏览 52
已采纳

将令牌方法添加到Yii内的登录表单

So i am trying to add the token method as an extra security feature in the login and what i did is the following

login.php

$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
if(Yii::app()->user->hasFlash('error')){
    ?>
    <div class="flashMessage">
        <?php echo Yii::app()->user->getFlash('error') ?>
    </div>
<?php
}
?>

    <input type="hidden" name="token" value="<?php echo $token ?>"/>
//Rest of the form

Now inside action Login I have the following.

// collect user input data
        if(isset($_POST['LoginForm'])){

            //Check if the token matches
            if($_POST['token'] == $_SESSION['token']){

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

Now when I try and log in i get the following error

Undefined variable: _SESSION

I don't understand why its telling that the session is not existing since it has the same name.

Any help i appricate

  • 写回答

1条回答 默认 最新

  • dtbonklcs575884485 2014-11-27 19:36
    关注

    Use SESSION in Yii like one of the following methods:

    1.Using Yii::app()->session

    Yii::app()->session['KEY']='VALUE'; //setting session 
    $key=Yii::app()->session['KEY']; //getting session
    

    2.Using Yii's States

    Yii::app()->user->setState('KEY','VALUE'); //setting session
    $key=Yii::app()->user->getState('KEY'); //getting session 
    

    But what is the differences between State and Session?

    If you use setState() | State, the probability of conflicting between applications will decrease by adding a hash, as it adds a hash inside the session KEY, for example if you get a dump from $_SESSION you will see:

    '0f42ca4d61b26371fee67cc51d7b48d3KEY' => 'VALUE' 
    

    But if you use Yii::app()->session it will store the plain KEY like below:

    'KEY' => 'VALUE'
    

    You may check SetState(), getState() and hasState()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。