我有几页需要登录,所以链接到这些页面的所有控制器都以
$this->checkSession();
//...rest of the code
Run Code Online (Sandbox Code Playgroud)
CheckSession应验证会话是否仍然有效,否则显示消息并停止执行控制器中的其余代码:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
.我期待这些指令按顺序发生,但我只得到一个空白页面.如何确保只在加载所有视图后才执行exit()?
Phi*_*eon 15
在这种情况下佩德罗是正确的.如果他们没有登录只是重定向它们,那么如果你可以使用Public/Admin命名的基本控制器来阻止你在每个单独的受保护文件中执行此操作,那就更好了.
但一般来说,如果使用exit(),它将停止输出库的运行.如果您只想停止当前控制器执行但允许输出控制器,则可以使用完全相同的方式返回.
function checkSession()
{
return (bool) $this->session->userdata('is_logged_in');
}
Run Code Online (Sandbox Code Playgroud)
那简单地说:
if(!$this->checkSession())
{
//the session has expired!
$data['main'] = 'confirmation_message';
$data['title'] = "Session expired";
$this->load->vars($data);
$this->load->view('template');
return;
}
Run Code Online (Sandbox Code Playgroud)
如果你真的希望应用程序的执行立即死亡以进行调试,错误报告等,那么应该只使用exit().
Ped*_*dro 14
在这种情况下,您不应该使用exit,如果会话无效,您应该使用示例重定向您的应用程序:
redirect('/init/login/','refresh');
Run Code Online (Sandbox Code Playgroud)
Kei*_*ter 12
我遇到了类似的问题.由于没有登录我想停止用户的地方.但是我想为他们提供一个链接列表,而不是简单地将它们重定向到登录页面.我正在使用CI版本1.7.2和$ this - > _ output()$ this-> display - > _ output()和$ this-> output - > _ display()解决方案对我不起作用.然而,我使用$ this-> output-> get_output()函数得到我的结果.
$this->load->vars($data);
$this->load->view('template');
die($this->output->get_output());
Run Code Online (Sandbox Code Playgroud)
$this->output->_display();
exit();
Run Code Online (Sandbox Code Playgroud)
是正确的答案!感谢Sam Sehnert ......它隐藏在评论中,所以我想重新发帖.