Php Codeigniter:标头已发送错误
我有一个具有身份验证控制器和switchuser功能的CI应用程序。基本上,它所做的一切都是从URI中获取一个ID,从ID中获取一些用户数据,分配一些会话数据,然后加载一个视图Php Codeigniter:标头已发送错误,php,codeigniter,http-headers,Php,Codeigniter,Http Headers,我有一个具有身份验证控制器和switchuser功能的CI应用程序。基本上,它所做的一切都是从URI中获取一个ID,从ID中获取一些用户数据,分配一些会话数据,然后加载一个视图 function switch_user(){ if(!$this->auth_lib->is_admin()){ $this->session->set_flashdata('status', 'You need to be an administrator to acce
function switch_user(){
if(!$this->auth_lib->is_admin()){
$this->session->set_flashdata('status', 'You need to be an administrator to access this page.');
redirect('auth/login');
}
if($id = $this->uri->segment(3)):
$this->load->model('auth_model', 'auth');
$username = $this->auth->get_username($id)->account_name;
$this->session->set_userdata(array('at_id' => $id, 'username' => $username));
$this->session->set_flashdata('status','User switched.');
endif;
$this->load->model('clients_model', 'clients');
$data['users'] = $this->clients->get_at_clients();
$this->load->view('auth/switch', $data);
}
我得到以下错误:
Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\cp\application\controllers\auth.php:130)
第130行是$username=$this->auth->get\u username($id)->account\u name
以下是模型函数:
function get_username($at_id){
$portal = $this->load->database('warehouse', TRUE);
$portal->select('account_name');
$portal->where('account_id', $at_id);
$portal->from('wh_account');
$query = $portal->get()->result();
return $query[0];
}
我不明白发生了什么事。当我在本地运行代码时,我没有得到错误。但是当我远程运行它时,也就是在互联网上运行时,我得到了这个错误
有人能帮忙找出问题吗
谢谢
比利
更新
实际上,我在$username=$this->auth->get\u username($id)->account\u name周围放置了一个var\u转储;
导致错误的行。我不知道为什么:(我会检查您的任何模型、控制器或库中是否没有关闭PHP标记-这通常会导致此类错误。
//您的代码用于重定向
//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>
';
重定向(“站点/功能1”);
//解决此问题的备用代码
$url='site/function1';
回声'
window.location.href=“'.base_url().'index.php?/'.$url.”;
';
能否验证您是否在本地启用了错误报告功能?您是否在
$query
中得到结果?是的,它正在返回结果。当我在本地运行时,用户切换成功,并且新用户名正确添加到sessionI set error|u reporting(E|u error | E| U WARNING | E| U PARSE | E| E|NOTICE)中;在切换发生之前,在控制器中,我没有收到任何错误感谢您的建议,只是检查了一下,所有的结束标记都在那里:(@iamjonesy-这是我的观点,不应该有任何结束标记(这可能会导致输出空白,从而触发标题).Hi BrynJ抱歉,我实际上添加了不正确的代码。我在这行$username=$this->auth->get\u username($id)周围有一个变量转储->account_name;一旦我删除了它,headers错误就停止了。我从代码粘贴中删除了它,因为我认为它与错误无关。为什么var_dump会导致错误消息?这种错误会在过早输出/打印数据时显示。您可以通过放置“die;”来修复它在var_转储之后。真的很有帮助!谢谢!我使用了第二种替代方法,即javascript,它工作得很好!比我尝试过的任何其他方法都好!谢谢