сеанс метода не существует в cakephp - PullRequest
0 голосов
/ 22 февраля 2019

Здесь я хочу показать переменную rand, rand1 в файле login.ctp, потому что я хочу создать капчу в дополнительном формате.но произошла ошибка "сеанс метода не существует".Я не знаю, как это решить.Надеюсь, что кто-нибудь может решить эту проблему

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/>
...