简体   繁体   中英

How to redirect to login page is session experies

Iam new to cakephp and iam using auth for my login utiities...I want to redirect to the login page incase of my session experies for all my actions ...I wrote code like\\

public function index() {        
    if(!$this->Session->read('username'))
        $this->logout();
    $this->set('users', $this->paginate());        
}

but for all my actions such as add(),edit()...every time I need to check for session variable...if I write the condition in __construct like

public function __construct()
{
    if(!$this->Session->read('username'))
        $this->logout();
}

it giving me error like

Error: Call to a member function read() on a non-object 

can anyone suggest me

Try

if (!$this->Session->valid()) {
  $this->logout();
}

Looks like you don't have included the SessionComponent.

Try to add

$components = array('Session');

in your AppController first.

Then check the documentation for more information: http://api.cakephp.org/2.3/class-SessionComponent.html

如果会话到期并且用户尝试访问受保护的页面,则使用AuthComponent ,他将被自动重定向到登录页面。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM