【发布时间】:2016-10-19 10:14:17
【问题描述】:
这是我在同一页面控制器中的登录和注销功能。当我在注销中使用 Session::destroy() 时,它会抛出一个错误“不在对象上下文中使用 $this”。并告诉我如何检查会话是否处于活动状态。 提前谢谢你
public function login(){
$session = $this->request->session();
$student12 = TableRegistry::get('users');
$email=$this->request->data('email');
$password=$this->request->data('password');
$query12 = $student12->find();
$query12->where(['email'=>$email]);
foreach($query12 as $datax)
{
if($datax['password']===$password&&$datax['email']===$email)
{
//Session::write($key, $value);
//Session::read($key);
$this->redirect(['controller'=>'Panal','action' => 'Home']);
}else{
$this->redirect(['controller'=>'Student','action' => 'index']);
}
}
}
public function logout(){
Session::destroy();
$this->redirect(['controller'=>'Panal','action' => 'Home']);
}
【问题讨论】:
-
您应该使用 Cakephp Auth 组件进行身份验证。 book.cakephp.org/3.0/en/controllers/components/…
-
如何选择表进行授权
-
默认情况下,Auth 组件使用带有凭据字段用户名和密码的“用户”表。您可以覆盖它。要了解更多信息,您必须阅读文档和本教程 (book.cakephp.org/3.0/en/tutorials-and-examples/…)。这很容易理解。
标签: session cakephp cakephp-3.0