Codeigniter 3 登录检查功能未显示正确的闪存数据消息
Codeigniter 3 login check function not showing correct flashdata messages
我正在创建一个登录检查功能。但是我的两条闪现数据信息设置不正确。
- 如果用户已经登录,然后如果会话过期,它应该设置这个
闪现数据消息 您的会话令牌已过期!
- 并且如果用户没有登录并尝试在未登录的情况下访问控制器
然后它应该设置这个 flashdata 消息 你需要登录
访问此站点!
出于某种原因,它总是显示第二条闪存数据消息。
Question: How am I able to use the two flashdata messages properly.
控制器: Login.php
功能:检查
public function check() {
$uri_route = basename($this->router->directory) .'/'. $this->router->fetch_class();
$route = isset($uri_route) ? $uri_route : '';
$ignore = array(
'common/login',
'common/forgotten',
'common/reset'
);
if (!in_array($route, $ignore)) {
// $this->user->is_logged() returns the user id
if ($this->user->is_logged()) {
// $this->session->userdata('is_logged') returns true or false
if (!$this->session->userdata('is_logged')) {
// Redirects if the user is logged on and session has expired!
$this->session->set_flashdata('warning', 'Your session token has expired!');
redirect('admin/common/login');
}
} else {
$this->session->set_flashdata('warning', 'You need to login to access this site!');
redirect('admin/common/login');
}
}
我 运行 通过 codeigniter 挂钩运行,这样我就不必在每个控制器上都添加它。
$hook['pre_controller'] = array(
'class' => 'Login',
'function' => 'check',
'filename' => 'Login.php',
'filepath' => 'modules/admin/controllers/common'
);
你想通过 uri() 检查什么实际上是一种非常糟糕的检查方式,你还应该将登录检查作为一个构造函数包括在内,而不是单一的。你的函数应该是这样的:
function __construct()
{
parent::__construct();
if (!$this->session->userdata('logged_in')) {
// Allow some methods?
$allowed = array(
'some_method_in_this_controller',
'other_method_in_this_controller',
);
if (!in_array($this->router->fetch_method(), $allowed)
{
redirect('login');
}
}
}
我正在创建一个登录检查功能。但是我的两条闪现数据信息设置不正确。
- 如果用户已经登录,然后如果会话过期,它应该设置这个 闪现数据消息 您的会话令牌已过期!
- 并且如果用户没有登录并尝试在未登录的情况下访问控制器 然后它应该设置这个 flashdata 消息 你需要登录 访问此站点!
出于某种原因,它总是显示第二条闪存数据消息。
Question: How am I able to use the two flashdata messages properly.
控制器: Login.php 功能:检查
public function check() {
$uri_route = basename($this->router->directory) .'/'. $this->router->fetch_class();
$route = isset($uri_route) ? $uri_route : '';
$ignore = array(
'common/login',
'common/forgotten',
'common/reset'
);
if (!in_array($route, $ignore)) {
// $this->user->is_logged() returns the user id
if ($this->user->is_logged()) {
// $this->session->userdata('is_logged') returns true or false
if (!$this->session->userdata('is_logged')) {
// Redirects if the user is logged on and session has expired!
$this->session->set_flashdata('warning', 'Your session token has expired!');
redirect('admin/common/login');
}
} else {
$this->session->set_flashdata('warning', 'You need to login to access this site!');
redirect('admin/common/login');
}
}
我 运行 通过 codeigniter 挂钩运行,这样我就不必在每个控制器上都添加它。
$hook['pre_controller'] = array(
'class' => 'Login',
'function' => 'check',
'filename' => 'Login.php',
'filepath' => 'modules/admin/controllers/common'
);
你想通过 uri() 检查什么实际上是一种非常糟糕的检查方式,你还应该将登录检查作为一个构造函数包括在内,而不是单一的。你的函数应该是这样的:
function __construct()
{
parent::__construct();
if (!$this->session->userdata('logged_in')) {
// Allow some methods?
$allowed = array(
'some_method_in_this_controller',
'other_method_in_this_controller',
);
if (!in_array($this->router->fetch_method(), $allowed)
{
redirect('login');
}
}
}