php 为什么我在 codeigniter 中收到“无法修改由 registration_model 发送的标头信息标头”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3395000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why I'm getting "cannot modify header information headers already sent by registration_model" error in codeigniter?
提问by Pavel
I have a problem with model in my codeigniter app. I'm getting this error of sending headers information. Basically, codeigniter is complaining about my registration model sending header information before anything else. How is it possible? I thought that models are only for holding db queries methods and nothing more. Can someone please explain me that?
我的 codeigniter 应用程序中的模型有问题。我收到发送标头信息的错误。基本上,codeigniter 抱怨我的注册模型先发送标头信息。这怎么可能?我认为模型仅用于保存 db 查询方法,仅此而已。有人可以向我解释一下吗?
This is how the beginning of a controller looks like:
这是控制器开头的样子:
function User()
{
parent::Controller();
$this->view_data['base_url'] = base_url();
$this->load->model('User_registration_model'); // don't forget capital, it's important
$this->load->model('user_map_model'); // don't forget capital, it's important
$this->load->model('Tribe_model'); // don't forget capital, it's important
$this->load->library('email'); // Loading email library
$this->load->library('session'); // sets up the session
$this->load->library ('form_validation'); // Loading form validation library
$this->load->helper(array('form', 'url'));
}
回答by Sarfraz
Note that headers should be sent beforeanything else. Make sure that there is no code/html or even space/indentation before the header function and there is nothing before the first opening php tag <?php
as well as ending tag ?>
in your view.
请注意,应在其他任何事情之前发送标头。确保标题函数之前没有代码/html 甚至空格/缩进,并且在您的视图中第一个打开的 php 标记<?php
和结束标记之前没有任何内容?>
。
回答by Gery
place this ob_start(); on first line of index.phpunder your application directory like this :
放置这个 ob_start(); 在您的应用程序目录下的index.php 的第一行,如下所示:
<?php
ob_start();
/*
*---------------------------------------------------------------
* APPLICATION ENVIRONMENT
*---------------------------------------------------------------
回答by Kaartikeyan R
Actually Some of Host Provider they are use PHP header();
to redirect our Site, So in that kind of servers if we use PHP header()
it will give the error. I think so. In Code Igniter redirect();
is using PHP header()
to redirect Our URL. So thats why it will give this Error!
实际上,一些主机提供商使用 PHPheader();
来重定向我们的站点,因此在这种服务器中,如果我们使用 PHP,header()
它会出现错误。我想是这样。在代码中,Igniterredirect();
使用 PHPheader()
来重定向我们的 URL。所以这就是为什么它会给出这个错误!
So the only solution is use JavaScript to Solve this Issue, I am Using it! Its working well.
所以唯一的解决方案是使用 JavaScript 来解决这个问题,我正在使用它!它运作良好。
//Your Code is for redirect
redirect('site/function1');
//Alternate Code for solve this issue
$url = 'site/function1';
echo'
<script>
window.location.href = "'.base_url().'index.php?/'.$url.'";
</script>
';
I don't know is this the right solution for the above issue, But I am using it, Its working 100%. Thanking you!
我不知道这是否是上述问题的正确解决方案,但我正在使用它,它 100% 工作。感谢您!