2

I have been getting a warning error all day yesterday and can't get it off. Don't know what is happening. it is complaining that some output was sent before execution of a redirect on property_model.php on line 1. I went there and checked for everything but no luck. I also checked for whitespaces. I am going to paste down the error and code.

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/sddhd/public_html/rsw/application/models/property_model.php:1)

Filename: helpers/url_helper.php

Line Number: 542

Now the code of model it shows above is:

<? class Property_model extends CI_Model {
    function __construct()
        {
            parent::__construct();
        }

        function insert_property($propertyID)
        {
            $this->property_id = $propertyID;
            $this->type = $_POST['property_type'];
            $this->city = $_POST['property_city'];
            $this->address = $_POST['property_address'];
            $this->description = $_POST['property_description'];
            $this->owner = $_POST['property_owner'];
            $this->date_added = date("Y-m-d");
            $this->price = $_POST['property_price'];
            $this->offer_type = $_POST['property_offer'];
            $this->contact = $_POST['property_contact'];
            $this->db->insert('property', $this);
        }

        function insert_image_details($id, $url, $extension)
        {
            $current->property_id = $id;
            $current->extension = $extension;
            $current->url = $url;
            $this->db->insert('photos', $current);
        }
    }
?>

and the uses of the redirect call (including whole controller call making calls to the model):

<?php
class Property extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
                $this->load->helper('url');
        $this->load->model('property_model', 'propertymodel');
        $this->load->database();
    }

    public function index() {
        $this->load->view('home');
    }

    public function new_property()
    {
        $this->load->view('new_property', array('error' => '' ));    
    }

    public function create_property()
    {
        //Pass the posted data to model for insertion
        $propertyID = uniqid();
        $this->propertymodel->insert_property($propertyID); 
        $this->do_upload($propertyID);
        redirect('/home');
    }


        public function do_upload($propertyId)
        {
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['max_size'] = '1000';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';

            $this->load->library('upload', $config);
                if ( ! $this->upload->do_upload())
                {
                    $error = array('error' => $this->upload->display_errors());

                    $this->load->view('create_property', $error);
                }
                else
                {
                    $data = array('upload_data' => $this->upload->data());
                    //array to access all the file uploaded attributes
                    $image_data = $this->upload->data();

                    //parameters to be passed to model: 
                    $imgUrl = "../.".$config['upload_path'].$image_data['file_name'];
                    //$imgTitle = $image_data['file_name'];
                    $fileExtension = $image_data['file_ext'];
                    $this->propertymodel->insert_image_details($propertyId, $imgUrl, $fileExtension);
                }           
        }//end of function


}
?>

Any help would be greatly appreciated

sys_debug
  • 3,883
  • 17
  • 67
  • 98

5 Answers5

3

I had the same problem. Nothing helped. Then I decided to create a complete new file and copied the content from the file that caused the error into the new one. This worked! Apparently the original file had a hidden byte at the position 0.

Pieter
  • 31
  • 1
2

You have mistake in Property controller. replace these two lines in the method create_property

$url= base_url().'home';
redirect($url);

with

redirect('/home');
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
Manish Ambalia
  • 359
  • 1
  • 8
0

Have you enabled error_reporting? The model might throw an error you don't see here. And this error causes the already sent headers.

By the way, where does $current in the method insert_image_details come from? That'll probably throw an error.

Louis Huppenbauer
  • 3,719
  • 1
  • 18
  • 24
  • nope that is not related. the current is simple an array and could be named anything. it contains data to be stored to database. – sys_debug Nov 02 '12 at 07:19
  • Actually that's an object and not an array, but you're right, that won't throw an error (didn't know that). – Louis Huppenbauer Nov 02 '12 at 07:23
  • I believe the error is coming because after I post data (form tag goes to property/create_property) the url never changes when I redirect. so when I refresh with that error on page, it still re-submits data. could that be the header info passed? and how can I solve it if yes? – sys_debug Nov 02 '12 at 07:27
  • Is there anything else in your error log? Another thing that just came to my mind - Is `$this->load->model` case sensitive or not (your model's called `Property_model` and not `property_model`)? – Louis Huppenbauer Nov 02 '12 at 07:30
  • Hm, I don't think that it's directly related to your post data. – Louis Huppenbauer Nov 02 '12 at 07:32
  • Nope Louis because I use a shorthand defined in the controller. It works fine because insertion to tables work and data is correct. So that's not the error for sure – sys_debug Nov 02 '12 at 07:32
  • If I change that to $this->load->view('home') it works well and redirects without error. but still if I refresh nit re-submits data because url still points to create_property method. Anyway for clearing that? – sys_debug Nov 02 '12 at 07:34
0

Might be some warning getting sent before the actual page is being load. Please turn on the error reporting using following functions in the index.php of your document root.

ini_set('display_errors', 'On');
error_reporting(E_ALL);
Kamrul
  • 7,175
  • 3
  • 31
  • 31
  • nope I placed it in the index.php same directory as application folder, system folder, etc. nothing shows – sys_debug Nov 02 '12 at 08:29
0

Found this alternative solution posted by another person in Stackoverflow.

 //Alternate Code for solve this issue
    $url = 'site/function1';
    echo'
    <script>
    window.location.href = "'.base_url().'index.php?/'.$url.'";
    </script>
    ';

This works really well! Hope there are no disadvantages to using this! Thanks all for support.

Codeigniter: headers already sent error

Community
  • 1
  • 1
sys_debug
  • 3,883
  • 17
  • 67
  • 98