简体   繁体   中英

Cannot modify header information in CI

I'm working on CodeIgniter using XAMPP. When I upload my files on my subdomain, it give me an error but its working on my local machine.

here is my controller

if ($this->session->userdata('admin_email') && $this->session->userdata('admin_id')) {
$data['title'] = $this->session->userdata('admin_name');
$this->load->view('admin/header',$data);
$this->load->view('admin/topbar');
$this->load->view('admin/sidebar');
//$this->load->view('admin/home');
$this->load->view('admin/footer');
} 
else 
{
$this->session->set_flashdata('adminlogin','please login first..');
redirect('admin/login');
}

here is my error:

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/shakzeec/public_html/demo/application/controllers/admin.php:2)

Filename: libraries/Session.php

Line Number: 688 A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/shakzeec/public_html/demo/application/controllers/admin.php:2)

Filename: libraries/Session.php

Line Number: 688 A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/shakzeec/public_html/demo/application/controllers/admin.php:2)

Filename: helpers/url_helper.php

Line Number: 542

PHP does not allow you to modify headers once output is already started. Redirecting requires a change to the header. If you want to redirect, do only a redirect and nothing else.

if (!$this->session->userdata('admin_email') && !$this->session->userdata('admin_id')) {
    $this->session->set_flashdata('adminlogin','please login first..');
    redirect('admin/login');
}
else{
    $data['title'] = $this->session->userdata('admin_name');
    $this->load->view('admin/header',$data);
    $this->load->view('admin/topbar');
    $this->load->view('admin/sidebar');
    //$this->load->view('admin/home');
    $this->load->view('admin/footer');
} 

Also, make sure there is nothing else in your controller which could be sending output before you modify the headers.

Here is the controllers/employers.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Employers extends CI_Controller {


        public function __construct() {
            parent::__construct();
            date_default_timezone_set("Asia/Manila"); 
            $this->load->model('_user');
        }

        public function index()
    {
            $data['userlist'] = $this->_user->get_user_list();
            $this->load->view('employers/index', $data);
            $this->template->render();
        }

        public function registration()
    {
            $data['userToken'] =$this->scriptor->userToken(); 
            $data['userlist'] = $this->_user->get_user_list();

            if(!isset($_REQUEST['msg'])){ $_REQUEST['msg'] = ''; }

            $this->load->view('employers/registration', $data);
            $this->template->render();

        }


        public function newregister()
    {
            $dr = strlen($_SERVER['PHP_SELF']) - 9;
            $path = $_SERVER["DOCUMENT_ROOT"] .substr($_SERVER['PHP_SELF'], 0, $dr) .'image/source/employer/' . strtolower($this->input->post('Username'));
            if(!is_dir($path)){
                //Make Directory
                if (!mkdir($path, 0777, true)) {
                    die('Failed to create folders...');
                }            
            }  
            $config['upload_path'] = $path;
            $config['file_name'] = url_title(strtolower($this->input->post('Username'))) . $this->upload->data('file_ext');
            $config['allowed_types'] = 'gif|jpg|png|';
            $config['max_size'] = '';
            $config['max_width'] = '';
            $config['max_height'] = '';
            $this->upload->initialize($config);
            $err = 'failed_reg';
            $ok = true;

           if(strlen($this->input->post('Username')) == 0){
                $ok = false;
                 $err = 'err_username';  
            }
            if(strlen($this->input->post('Lastname')) == 0 && $ok){
                $ok = false;
                $err = 'err_lastname';
            }
            if(strlen($this->input->post('Firstname')) == 0 && $ok){
                $ok = false;
                $err = 'err_firstname';
            }
            if(strlen($this->input->post('Email')) == 0 && $ok){
                $ok = false;
                $err = 'err_email';
            }
            if(strlen($this->input->post('Cellphone')) == 0 && $ok){
                $ok = false;
                $err = 'err_cellphone';
            }
            if(strlen($this->input->post('Telephone')) == 0 && $ok){
                $ok = false;
                $err = 'err_telephone';
            }
            if(strlen($this->input->post('Address')) == 0 && $ok){
                $ok = false;
                $err = 'err_address';
            }
            if(strlen($this->input->post('City')) == 0 && $ok){
                $ok = false;
                $err = 'err_city';
            }


                if(!$this->upload->do_upload('EmpPic') && $ok){
                     $ok = false;
                     if(is_dir($path)){
                         array_map('unlink', glob($path."/*"));
                         rmdir($path);
                     }
                    $err = 'err_image';
                 }
            if($_FILES['EmpGovID']['size'] > 0){
                if(!$this->upload->do_upload('EmpGovID') && $ok){
                     $ok = false;
                     if(is_dir($path)){
                         array_map('unlink', glob($path."/*"));
                         rmdir($path);
                     }
                    $err = 'err_image2';
                 }
            }

            if($ok){ 

            $i = array(
                         'Firstname'  => $this->scriptor->_enc($this->input->post('Firstname'))
                       , 'Middlename' => $this->scriptor->_enc($this->input->post('Middlename'))
                       , 'Lastname'   => $this->scriptor->_enc($this->input->post('Lastname'))
                       , 'Telephone'  => $this->scriptor->_enc($this->input->post('Telephone'))
                       , 'Cellphone'  => $this->scriptor->_enc($this->input->post('Cellphone'))
                       , 'Email'      => $this->scriptor->_enc($this->input->post('Email'))
                       , 'Address'    => $this->scriptor->_enc($this->input->post('Address'))
                       , 'City'       => $this->scriptor->_enc($this->input->post('City'))
                       , 'EPicture'   => base_url('image/employer') . '/'.$config['file_name']
                       , 'Username'   => $this->scriptor->_enc($this->input->post('Username'))
                       , 'Password'   => $this->scriptor->_enc($this->input->post('Password'))
                       , 'Token'      => $this->scriptor->userToken()
                       , 'Level'      => 'user'
                       , 'Yatype'     => $this->scriptor->_enc($this->input->post('Yatype'))
                       , 'GovID'      => base_url('image/employer') . '/'.$config['file_name']
                       , 'q1'         => $this->scriptor->_enc($this->input->post('q1'))
                       , 'q2'         => $this->scriptor->_enc($this->input->post('q2'))
                       , 'q3'         => $this->scriptor->_enc($this->input->post('q3'))
                       , 'aq1'        => $this->scriptor->_enc($this->input->post('aq1'))
                       , 'aq2'        => $this->scriptor->_enc($this->input->post('aq2'))
                       , 'salary'     => $this->scriptor->_enc($this->input->post('salary'))
                       , 'registeredDate' => date('Y-m-d h:i:s')
                       , 'Message'    => $this->scriptor->_enc($this->input->post('Message'))
                    );

                $this->_user->add($i);

                redirect('employers/registration?msg=success_reg');

            }  else {

                redirect('employers/registration?msg='. $err);

            }

            /******************************************************************/

        }//End Class

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM