CodeIgniter:如果我使用 die() 函数,视图不会加载

发布于 2024-09-14 06:57:40 字数 370 浏览 2 评论 0原文

我有以下代码。检查用户是否登录。 当变量 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤独难免 2024-09-21 06:57:40

实际上,我找到了保留 URL 而不是重定向的答案:

$data['main_content'] = 'unauthorized_access';
$this->load->view('includes/template', $data);

// Force the CI engine to render the content generated until now    
$this->CI =& get_instance(); 
$this->CI->output->_display();

die();

Actually, I've found an answer to mantain the URL and not redirect:

$data['main_content'] = 'unauthorized_access';
$this->load->view('includes/template', $data);

// Force the CI engine to render the content generated until now    
$this->CI =& get_instance(); 
$this->CI->output->_display();

die();
心欲静而疯不止 2024-09-21 06:57:40

反正。我使用了登录页面的重定向和 flashdata 变量,

if(!isset($is_logged_in) OR $is_logged_in == FALSE)
   {
       $this->session->set_flashdata('error_msg','You must be logged in to access restricted area');
       redirect('login/');
   }

谢谢

Anyway. I used a redirect to the login page, and a flashdata variable

if(!isset($is_logged_in) OR $is_logged_in == FALSE)
   {
       $this->session->set_flashdata('error_msg','You must be logged in to access restricted area');
       redirect('login/');
   }

Thanks

情愿 2024-09-21 06:57:40

我已经搞乱这个有一段时间了。如果您在尝试加载视图后使用 dieexit,CI 会显示空白页面。

解决方案是使用 return,它会停止当前函数的执行,并且不会执行该函数之后的任何内容。

一个简单的例子:

public function validate(){
 //validation code

 if(!$valid){
  $this->load->view('error');
  return;
 }

 //This code won't run
}

I've been messing around with this for a while. If you're using die or exit 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:

public function validate(){
 //validation code

 if(!$valid){
  $this->load->view('error');
  return;
 }

 //This code won't run
}
奈何桥上唱咆哮 2024-09-21 06:57:40

CI 可能正在使用输出缓冲(请参阅 http://www.php.net/手册/en/ref.outcontrol.php)。如果您想加载视图并终止脚本,则需要刷新缓冲区。这通常在脚本的最后完成,但 die()ing 阻止了它到达那里。

if(!isset($is_logged_in) OR $is_logged_in == FALSE)
{
    $data['main_content'] = 'not_logged_in';
    $data['data'] = '';
    $this->load->view('includes/template',$data);

    ob_flush();
    die();
}

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.

if(!isset($is_logged_in) OR $is_logged_in == FALSE)
{
    $data['main_content'] = 'not_logged_in';
    $data['data'] = '';
    $this->load->view('includes/template',$data);

    ob_flush();
    die();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文