2013-07-04 24 views

回答

3

假設您使用AuthComponent進行用戶會話,可以使用靜態user方法檢查用戶會話是否存在。這也包括在the documentation中。例如:

if (AuthComponent::user()): 
    // The user is logged in, show the logout link 
    echo $this->Html->link('Log out', array('controller' => 'users', 'action' => 'logout')); 
else: 
    // The user is not logged in, show login link 
    echo $this->Html->link('Log in', array('controller' => 'users', 'action' => 'login')); 
endif; 
+0

我試過這個解決方案的第一次,但我認爲AuthComponent :: user()對象在cookie中保存用戶詳細信息。因此,一旦登錄,用戶將始終看到註銷鏈接,直到瀏覽器cookie被清除。你有什麼想法或解決方案嗎? ??? – Sourabh

+0

@JohnHogan'Unexpected if'意思是你忘記在if之前終止前面的陳述。 – Oldskool

0

調用的註銷功能就像

function logout(){ 
    $this->Cookie->destroy('Your Cookie name goes here'); 
    if($this->Auth->logout($this->data)){ 
     //auto redirected 
    } 
} 
2

嘿這裏是我做到了在CakePHP 3.X

<?php 
if($this->request->Session()->read('Auth')) { 
    // user is logged in, show logout..user menu etc 
    echo $this->Html->link(__('Log Out'), ['controller' => 'Users', 'action' => 'logout']); 
} else { 
    // the user is not logged in 
    echo $this->Html->link(__('Log in'), ['controller' => 'Users', 'action' => 'login']); 
} 
?>