Why is this code trying to send header information

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rythmic
    New Member
    • Feb 2010
    • 29

    Why is this code trying to send header information

    Problem: I am trying to do a header("locatio n: $url"); call.

    Here is the error message:
    Warning: Cannot modify header information - headers already sent by (output started at D:\dev\web\brfc irkusprinsessan \dev\classes\Ne wsItem.php:1) in D:\dev\web\brfc irkusprinsessan \dev\handlers\f orm_handler.php on line 53
    The header location call is the line 53 in the error message. Below is the code for the NewsItem class. If I comment the usage of the NewsItem class everything works fine, so logic suggests something is wrong in the NewsItem class, but I don't see it.

    The error appears even if I don't use the if statement where the NewsItem class is used.

    Does anyone see the problem?

    first here is the form_handler.ph p code which has the error on line 54

    Code:
    <?php
    session_start();
    
    require_once '../classes/Calendar.php';
    require_once '../classes/DBEasy.php';
    require_once '../classes/Email.php';
    require_once '../classes/NewsHolder.php';
    require_once '../classes/NewsItem.php';
    
        $widget = $_POST['widget'];
        $action = $_POST['action'];
        $db_id = $_POST['dbid'];
        $location = getenv('HTTP_REFERER');
        // *** dispatcher code ***
        //
        // calendar update
        if ( $widget == 'calendar' && $action == 'update') {
            $cal = new Calendar($db_id);
            $cal->set_title($_POST['title']);
            $cal->set_description($_POST['desc']);
            $cal->save();
        }
        else if ($action=='doemail') {
            $to = 'styrelsen@example.com';
            $sub = 'Nytt kontaktmail - ' . $_POST['sub'];
            $msg = $_POST['msg'];
            $sender = 'hemsidan@example.com';
            $sender_name = 'Hemsidans kontaktform';
            //construct($recipient, $subject,$message,$sender,$sender_name='',$recipient_name='')
            $email = new Email($to,$sub,$msg,$sender,$sender_name);
    
            Email::send($email);
        }
        else if ( $widget == 'newsholder' && $action == 'update') {
            $nh = new NewsHolder($db_id);
            $nh->set_title($_POST['title']);
            $nh->set_description($_POST['desc']);
            $nh->set_max_news_displayed($_POST['max_news_displayed']);
            $nh->set_max_news_message_length($_POST['max_news_message_length']);
            $nh->save();
        }
        
        else if ( $widget == 'newsitem' && $action == 'update') {
            $ni = new NewsItem($db_id);
            $ni->set_title($_POST['title']);
            $ni->set_message($_POST['message']);
            //TODO: add image id handling
            $ni->save();
    
        }
    
        // after updating the data model go back to referring page
        header('Location: ' . $location);
    ?>

    And here's the NewsItem class
    Code:
     <?php
        class NewsItem {
    
            private $db_id;
            private $newsholder_id;
            private $author_id;
            private $creation_date;
            private $title;
            private $message;
            private $image_url;
    
            private $editable = false;
            private $use_lightbox_view = true;
            private $use_lightbox_edit = true;
    
            public function __construct($db_id=-1) {
    
                if ( $db_id != -1 ) {
                    $this->fetch($db_id);
    
                    if ( is_null( $this->db_id ) ) {
                        // handle error
                    }
                  }
              }
    
              public function get_db_id() {
                    return $this->db_id;
              }
    
              public function get_newsholder_id() {
                  return $this->newsholder_id;
              }
    
              public function set_newsholder_id($newsholder_id) {
                  $this->newsholder_id = $newsholder_id;
              }
    
              public function get_author_id() {
                return $this->author_id;
              }
    
              public function set_author_id($author_id) {
                $this->author_id = $author_id;
              }
    
              public function get_creation_date() {
                return $this->creation_date;
              }
    
              public function get_title() {
                return $this->title;
              }
    
              public function set_title($title) {
                $this->title = $title;
              }
    
              public function get_message() {
                return $this->message;
              }
    
              public function set_message($message) {
                $this->message = $message;
              }
    
              public function get_image_url() {
                return $this->image_url;
              }
    
              public function set_image_url($url) {
                $this->image_url = $url;
              }
    
              public function get_editable() {
                  return $this->editable;
              }
    
              public function set_editable($editable) {
                  $this->editable = $editable;
              }
    
    
              /**
              * @desc populates this NewsItem with data from the News containing the given
              * db_id;
              */
              private function fetch($db_id) {
                $tbl = 'news_items';
                $cols = array('news_item_id','news_holder_id', 'author_id', 'creation_date', 'title','message','image_url');
                $db = new DBEasy($tbl, $cols);
    
                $rows = $db->conditional_select(array(array('news_item_id','=','"' . $db_id .'"' )));
    
    
                //if results - add values to fields
                if ( count($rows) > 0 ) {
                    $this->db_id= $rows[0]['news_item_id'];
                    $this->newsholder_id = $rows[0]['news_holder_id'];
                    $this->author_id= $rows[0]['author_id'];
                    $this->creation_date= $rows[0]['creation_date'];
                    $this->title = $rows[0]['title'];
                    $this->message = $rows[0]['message'];
                    $this->image_url = $rows[0]['image_url'];
                }
    
              }
    
              public function save() {
                  $tbl = 'news_items';
                  $cols = array('news_item_id','news_holder_id', 'author_id', 'creation_date', 'title','message','image_url');
                  $value_pairs = array('news_holder_id'=>$this->newsholder_id, 'author_id'=>$this->author_id, 'creation_date'=>$this->creation_date, 'title'=>$this->title,'message'=>$this->message,'image_url'=>$this->image_url);
                  $db = new DBEasy($tbl, $cols);
    
                  //insert
                if (is_null($this->db_id) || $this->db_id == -1) {
                    $success = $db->insert($value_pairs);
                }
                //update
                else {
                    $success = $db->conditional_update($value_pairs, array(array( 'news_item_id','=',$this->db_id)));
                }
    
              }
    
              public function get_date($style=1) {
                  if ($style == 1)
                    return substr($this->creation_date, 0,10);
                  else if ($style == 2) {
                      return substr($this->creation_date,9,1) . '/' . substr($this->creation_date,6,2) . substr($this->creation_date, 0,4);
                  }
                  else if ($style == 3) {
                    return substr($this->creation_date,9,1) . ' ' . self::get_month_name(substr($this->creation_date,6,2)) . ' ' . substr($this->creation_date, 0,4);
                  }
                  else if ($style == 4) {
                    return substr($this->creation_date,9,1) . ' ' . self::get_month_name(substr($this->creation_date,6,2),true) . ' ' . substr($this->creation_date, 0,4);
                  }
    
              }
    
              public static function get_month_name($nbr, $full_name= false) {
                  if (!$full_name)
                    $month_names = array('jan','feb','mar','apr','maj','jun','jul','aug','sep','okt','nov','dec');
                  else
                    $month_names = array('januari','februari','mars','april','maj','juni','juli','augusti','september','oktober','november','december');
                  return $month_names[$nbr-1];
            }
    
            public function present_message($max_length = 0) {
                if ( $max_length == 0)
                    return $this->message;
    
                $disallowed_end_arr = array(',','.','!','?','-',';',':'."'");
                if ( strlen($this->message) > $max_length ) {
    
                    for($i = $max_length; $i > 0; $i--) {
                        if ( strcmp(substr($this->message,$i,1),' ') === 0 )
                            if ( strcmp(substr($this->message,($i-1),1),',' ) === 0 ) {
                                return substr($this->message,0,$i-1) . '...';
                            }
                            else
                                return substr($this->message,0,$i) . '...';
                    }
                }
                else
                    return $this->message;
            }
    
    
              public function render($max_length = 0) {
    
                    if ( $this->use_lightbox_edit)
                        $class = 'lbOn';
                    else
                        $class = '';
                    $edit_link = '<a class="' . $class . '" href="adm_edit_news_item.php?ni=' . $this->db_id . '">Redigera</a>';
                    $code = array();
    
                    $code[] = '<div class="news_item" style="border:1px solid #000;margin:10px 3px;padding:0;">' ."\n";
                    $code[] = '<div class="news_item_header" style="background-color:#000;border-bottom:1px solid #000;">';
                    $code[] = '<h5 class="news_date" style="color:#555;font-family:verdana;font-size:10px;font-style:italic;padding: 2px 5px 2px 10px;;margin:0;text-align:right;">' . $this->get_date(4) . "</h5>";
                    $code[] = '<h4 class="news_rubrik" style="color:#ddd;font-family:arial;font-size:12px;font-weight:bold;padding:2px 10px 2px 5px;margin:0;">' . $this->title . '</h4>';
                    if ($this->editable)
                        $code[] = '<h4 class="news_rubrik" style="color:#ddd;font-family:arial;font-size:12px;font-weight:bold;padding:2px 10px 2px 5px;margin:0;">' . $edit_link . '';
    
                    $code[] = "</div>";
                    $code[] = '<div class="news_text" style="background-color:#eee;padding: 3px 5px;">' . $this->present_message($max_length);
                    //$code[] = '<div class="news_text" style="background-color:#f00;padding: 3px 5px; color:#fff;">' . $max_length;
                    if ($max_length != 0 && strlen($this->message) > $max_length) {
                        if ($this->use_lightbox_view)
                                $class = 'lbOn';
                        else
                            $class = '';
                        $code[] = '<br /><a class="'.$class.'" href="nyheter.php?nh=' . $this->newsholder_id . '&id=' . $this->db_id . '" title="L&auml;s hela nyheten: ' . $this->title . '">L&auml;s mer...</a>';
                    }
                    $code[] = "</div>";
                    $code[] = "</div>";
    
                    return join("\n", $code);
              }
    
              public function render_thumbnail_image() {
    
              }
    
          }
    ?>
    Best regards
    Rythmic
    Last edited by Atli; Feb 24 '10, 11:50 AM. Reason: Replaced real email addresses with example values.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    see CNP: Headers already sent (Bytes Articles)

    Comment

    • rythmic
      New Member
      • Feb 2010
      • 29

      #3
      Solved

      Killer article. Thanks Dormilich and Markus

      It was one of the more rare causes, an accidental whitespace before the <?php opening tag.

      Comment

      Working...