CodeIgniter:如果我使用 die() 函数,视图不会加载
我有以下代码。检查用户是否登录。 当变量 $is_logged_in 未设置或为 False 时,我加载消息视图。 不幸的是,系统同时加载了受限的内容视图。 所以我使用了 die() 函数,现在只显示一个空白页。
如何才能仅在用户未登录时加载消息视图? 谢谢。
if(!isset($is_logged_in) OR $is_logged_in == FALSE)
{
$data['main_content'] = 'not_logged_in';
$data['data'] = '';
$this->load->view('includes/template',$data);
die();
}
I have the following code. Checks if the user is logged in or not.
When the variable $is_logged_in is not set or is False, I load a message view.
Unfortunately, at the same time the system loads the restricted content view.
So I used die() function, and now only shows a blank page.
What can I do to only load the message view when the user is not logged in?
Thanks.
if(!isset($is_logged_in) OR $is_logged_in == FALSE)
{
$data['main_content'] = 'not_logged_in';
$data['data'] = '';
$this->load->view('includes/template',$data);
die();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上,我找到了保留 URL 而不是重定向的答案:
Actually, I've found an answer to mantain the URL and not redirect:
反正。我使用了登录页面的重定向和 flashdata 变量,
谢谢
Anyway. I used a redirect to the login page, and a flashdata variable
Thanks
我已经搞乱这个有一段时间了。如果您在尝试加载视图后使用
die
或exit
,CI 会显示空白页面。解决方案是使用
return
,它会停止当前函数的执行,并且不会执行该函数之后的任何内容。一个简单的例子:
I've been messing around with this for a while. If you're using
die
orexit
after trying to load a view, CI shows a blank page.The solution would be to use
return
, which stops execution of the current function, and does not execute anything after below that.A simple example:
CI 可能正在使用输出缓冲(请参阅 http://www.php.net/手册/en/ref.outcontrol.php)。如果您想加载视图并终止脚本,则需要刷新缓冲区。这通常在脚本的最后完成,但 die()ing 阻止了它到达那里。
CI is probably using output buffering (see http://www.php.net/manual/en/ref.outcontrol.php). If you want to load a view and kill the script, you will need to flush the buffer. This would normally be done at the very end of the script, but die()ing stops it from getting there.