1
votes

I have a visitor logging script that I want to be run on all pages
so I kept it in appController's beforeFilter method as below

public function beforeFilter() { 
    //log visitors starts here
    if( $this->Session->check('visited') == true )
    {
        //logging code
    }
    else
    {
        $this->Session->write('visited') = true;  //this line gives error
    }
    //log visitors ends here
}

I am getting the following error

 Fatal error: Class declarations may not be nested in D:\wamp\www\myproject\lib\Cake\Error\ExceptionRenderer.php on line 54

by commenting session writing line shown below, page runs without ant error

$this->Session->write('visited') = true;    

I alse changed this code to afterFilter and beforeRender methods but still error

2

2 Answers

2
votes

According to the documentation : http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::write

public function beforeFilter() { 
    //log visitors starts here
    if( $this->Session->check('visited') ) // No need to check == true
    {
        //logging code
    }
    else
    {
        $this->Session->write('visited', true);  //Second param to set the value
    }
    //log visitors ends here
}
0
votes

You can use CakeSession instead:

public function beforeFilter() { 
    if( CakeSession::check('visited') ){
        //logging code
    } else {
        CakeSession::write('visited', true);
    }
}

source: https://book.cakephp.org/2.0/en/development/sessions.html