在cakephp中不存在方法会话

时间:2019-02-22 06:32:00

标签: cakephp cakephp-2.0

在这里,我想在login.ctp文件中显示变量rand,rand1,因为我想以其他格式创建验证码。但是发生了错误“方法会话不存在”。我不知道该怎么解决。希望任何人都可以解决此问题

public function login() {
//craete session
    $session = $this->request->session();
    //read session
    $rand =$session->read('rand');
    $this->set('rand',$rand);
    $rand1=$session->read('rand1');
    $this->set('rand1',$rand1);
    $sum = $session->read('sum');
    $this->set('sum',$sum);

    //if already logged-in, redirect
    if($this->Session->check('Auth.User')){
        return $this->redirect(array('action' => 'dashboard'));     
    }

    // if we get the post information, try to authenticate
    if ($this->request->is('post')) {

        $session->write('rand', rand(1,9));
        $session->write('rand1', rand(1,9));
        $session->write('sum', $rand + $rand1);
        if ($this->Auth->login()) {

            $status = $this->Auth->user('status');
            if($status != 0){
                $this->Session->setFlash(__('Welcome, '. $this->Auth->user('username')));
                return $this->redirect($this->Auth->redirectUrl());
            }else{
                // this is a deleted user
                $this->Auth->logout();
                $this->Session->setFlash(__('Invalid username or password - This user appears to be deleted...'));
            }
        } else {
            $this->Session->setFlash(__('Invalid username or password'));
        }
    } 
}

login.ctp

 <div class="users form">
    <?php echo $this->Session->flash('auth'); ?>
    <?php echo $this->Form->create('User'); ?>
        <fieldset>
            <legend><?php echo __('Please enter your username and password'); ?></legend>
            <?php echo $this->Form->input('username');
            echo $this->Form->input('password');
        ?>
            <label><?php echo $rand; ?> + <?php echo $rand; ?></label>
        </fieldset>
    <?php echo $this->Form->end(__('Login')); ?>
    <small>If you have just installed this application, <?php  echo $this->Html->link( "Click here to setup the CMS",   array('controller'=>'users','action'=>'setup') );  ?></small>
    </div>
    <?php  echo $this->Html->link( "Back to the main site",   array('controller'=>'posts','action'=>'index') );  ?>
    <br/>

0 个答案:

没有答案