gandy 发表于 2013-11-6 18:19:59

session错误

本帖最后由 gandy 于 2013-11-6 18:26 编辑

我用的是CI自带的SESSION;

在autoload.php里面加载$autoload['libraries'] = array('database', 'session');
在config设置了key

每次登录后5分钟,页面刷新就出现以下错误

A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at E:\PHP\APMServ-v5.2.6\APMServ5.2.6\www\htdocs\ci\application\controllers\admin\main.php:1)
Filename: libraries/Session.php
Line Number: 675
我的LOGIN控制器代码如下:
class Login extends CI_Controller {
      function__construct()
      {
      parent::__construct();
    }
      
      public function index()
      {
                if ($this->session->userdata('username')) {
                redirect('admin/main');
            }
            
            $this->load->library('form_validation'); // 使用CI的表单验证, 如下:
            $this->form_validation->set_rules('username', '用户名', 'min_length|required');
            $this->form_validation->set_rules('password', '密码', 'min_length|required');
            
            if($this->form_validation->run() !== false){
                // then validate password. Get from the Db.
                $this->load->model('m_admin_login');
                $res = $this->m_admin_login->check_user(
                                                $this->input->post('username'),
                                                $this->input->post('password')
                                          );
                if($res !== false){
                  //print_r($res);
                                       
                                    $this->session->set_userdata('username',$this->input->post('username'));
                                        //$this->session->set_userdata('userid',res['a_id']);
                  redirect('admin/main');   
                }
            }
                $this->load->view('admin/login');
      }
      public function out_login()
      {
                $this->session->unset_userdata('username');
                //$this->session->sess_destroy();
                redirect('admin/login');   
      }
}

控制器main,也就是登陆后的控制器代码如下:
class Main extends CI_Controller {
      function__construct()
      {
      parent::__construct();
    }

      
      public function index()
      {      
                           
                //$this->load->library();
                if ($this->session->userdata('username'))
                        {
                  $data['username']=$this->session->userdata('username');
                              $this->load->view('admin/main',$data);
            }
                else
                        {
                              $this->load->view('admin/login');
                                 //redirect('admin/login');
                        }
      }

}

大家帮我看看错在哪里啊


gandy 发表于 2013-11-6 23:16:50

经过南宫大侠的远程帮助~·原来
原来
原来是BOM作怪
去除BOM就OK了

Ahgigu 发表于 2013-11-7 16:44:06

很像文件格式的问题
页: [1]
查看完整版本: session错误