我有几个页面需要登录,所以所有链接到这些页面的 Controller 都以
开头$this->checkSession();
//...rest of the code
CheckSession 应该验证 session 是否仍然存在,否则显示一条消息并停止执行 Controller 中其余代码:
function checkSession()
{
if (!$this->session->userdata('is_logged_in'))
{
//the session has expired!
$data['main'] = 'confirmation_message';
$data['title'] = "Session expired";
$this->load->vars($data);
$this->load->view('template');
exit();
}
}
. 我原以为这些指令会按顺序发生,但我只得到一个空白页。 如何确保仅在加载所有 View 后才执行 exit()?
最佳答案
在这种情况下,佩德罗是正确的。如果他们没有登录只是重定向他们,如果你可以使用Public/Admin named base controllers就更好了。以阻止您必须在每个单独的 protected 文件中执行此操作。
不过,一般来说,如果您使用 exit(),它将停止运行输出库。如果您只想阻止当前 Controller 执行但允许 Controller 输出,您可以使用完全相同的方式返回。
function checkSession()
{
return (bool) $this->session->userdata('is_logged_in');
}
那么简单:
if(!$this->checkSession())
{
//the session has expired!
$data['main'] = 'confirmation_message';
$data['title'] = "Session expired";
$this->load->vars($data);
$this->load->view('template');
return;
}
exit() 应该只在你真的想立即终止应用程序的执行以进行调试、错误报告等时使用。
关于PHP/codeigniter - 使用 exit(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2618993/