By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,352 Members | 1,593 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,352 IT Pros & Developers. It's quick & easy.

Why is this code trying to send header information

P: 29
Problem: I am trying to do a header("location: $url"); call.

Here is the error message:
Warning: Cannot modify header information - headers already sent by (output started at D:\dev\web\brfcirkusprinsessan\dev\classes\NewsIte m.php:1) in D:\dev\web\brfcirkusprinsessan\dev\handlers\form_h andler.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.php code which has the error on line 54

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3.  
  4. require_once '../classes/Calendar.php';
  5. require_once '../classes/DBEasy.php';
  6. require_once '../classes/Email.php';
  7. require_once '../classes/NewsHolder.php';
  8. require_once '../classes/NewsItem.php';
  9.  
  10.     $widget = $_POST['widget'];
  11.     $action = $_POST['action'];
  12.     $db_id = $_POST['dbid'];
  13.     $location = getenv('HTTP_REFERER');
  14.     // *** dispatcher code ***
  15.     //
  16.     // calendar update
  17.     if ( $widget == 'calendar' && $action == 'update') {
  18.         $cal = new Calendar($db_id);
  19.         $cal->set_title($_POST['title']);
  20.         $cal->set_description($_POST['desc']);
  21.         $cal->save();
  22.     }
  23.     else if ($action=='doemail') {
  24.         $to = 'styrelsen@example.com';
  25.         $sub = 'Nytt kontaktmail - ' . $_POST['sub'];
  26.         $msg = $_POST['msg'];
  27.         $sender = 'hemsidan@example.com';
  28.         $sender_name = 'Hemsidans kontaktform';
  29.         //construct($recipient, $subject,$message,$sender,$sender_name='',$recipient_name='')
  30.         $email = new Email($to,$sub,$msg,$sender,$sender_name);
  31.  
  32.         Email::send($email);
  33.     }
  34.     else if ( $widget == 'newsholder' && $action == 'update') {
  35.         $nh = new NewsHolder($db_id);
  36.         $nh->set_title($_POST['title']);
  37.         $nh->set_description($_POST['desc']);
  38.         $nh->set_max_news_displayed($_POST['max_news_displayed']);
  39.         $nh->set_max_news_message_length($_POST['max_news_message_length']);
  40.         $nh->save();
  41.     }
  42.  
  43.     else if ( $widget == 'newsitem' && $action == 'update') {
  44.         $ni = new NewsItem($db_id);
  45.         $ni->set_title($_POST['title']);
  46.         $ni->set_message($_POST['message']);
  47.         //TODO: add image id handling
  48.         $ni->save();
  49.  
  50.     }
  51.  
  52.     // after updating the data model go back to referring page
  53.     header('Location: ' . $location);
  54. ?>
  55.  
  56.  

And here's the NewsItem class
Expand|Select|Wrap|Line Numbers
  1.  <?php
  2.     class NewsItem {
  3.  
  4.         private $db_id;
  5.         private $newsholder_id;
  6.         private $author_id;
  7.         private $creation_date;
  8.         private $title;
  9.         private $message;
  10.         private $image_url;
  11.  
  12.         private $editable = false;
  13.         private $use_lightbox_view = true;
  14.         private $use_lightbox_edit = true;
  15.  
  16.         public function __construct($db_id=-1) {
  17.  
  18.             if ( $db_id != -1 ) {
  19.                 $this->fetch($db_id);
  20.  
  21.                 if ( is_null( $this->db_id ) ) {
  22.                     // handle error
  23.                 }
  24.               }
  25.           }
  26.  
  27.           public function get_db_id() {
  28.                 return $this->db_id;
  29.           }
  30.  
  31.           public function get_newsholder_id() {
  32.               return $this->newsholder_id;
  33.           }
  34.  
  35.           public function set_newsholder_id($newsholder_id) {
  36.               $this->newsholder_id = $newsholder_id;
  37.           }
  38.  
  39.           public function get_author_id() {
  40.             return $this->author_id;
  41.           }
  42.  
  43.           public function set_author_id($author_id) {
  44.             $this->author_id = $author_id;
  45.           }
  46.  
  47.           public function get_creation_date() {
  48.             return $this->creation_date;
  49.           }
  50.  
  51.           public function get_title() {
  52.             return $this->title;
  53.           }
  54.  
  55.           public function set_title($title) {
  56.             $this->title = $title;
  57.           }
  58.  
  59.           public function get_message() {
  60.             return $this->message;
  61.           }
  62.  
  63.           public function set_message($message) {
  64.             $this->message = $message;
  65.           }
  66.  
  67.           public function get_image_url() {
  68.             return $this->image_url;
  69.           }
  70.  
  71.           public function set_image_url($url) {
  72.             $this->image_url = $url;
  73.           }
  74.  
  75.           public function get_editable() {
  76.               return $this->editable;
  77.           }
  78.  
  79.           public function set_editable($editable) {
  80.               $this->editable = $editable;
  81.           }
  82.  
  83.  
  84.           /**
  85.           * @desc populates this NewsItem with data from the News containing the given
  86.           * db_id;
  87.           */
  88.           private function fetch($db_id) {
  89.             $tbl = 'news_items';
  90.             $cols = array('news_item_id','news_holder_id', 'author_id', 'creation_date', 'title','message','image_url');
  91.             $db = new DBEasy($tbl, $cols);
  92.  
  93.             $rows = $db->conditional_select(array(array('news_item_id','=','"' . $db_id .'"' )));
  94.  
  95.  
  96.             //if results - add values to fields
  97.             if ( count($rows) > 0 ) {
  98.                 $this->db_id= $rows[0]['news_item_id'];
  99.                 $this->newsholder_id = $rows[0]['news_holder_id'];
  100.                 $this->author_id= $rows[0]['author_id'];
  101.                 $this->creation_date= $rows[0]['creation_date'];
  102.                 $this->title = $rows[0]['title'];
  103.                 $this->message = $rows[0]['message'];
  104.                 $this->image_url = $rows[0]['image_url'];
  105.             }
  106.  
  107.           }
  108.  
  109.           public function save() {
  110.               $tbl = 'news_items';
  111.               $cols = array('news_item_id','news_holder_id', 'author_id', 'creation_date', 'title','message','image_url');
  112.               $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);
  113.               $db = new DBEasy($tbl, $cols);
  114.  
  115.               //insert
  116.             if (is_null($this->db_id) || $this->db_id == -1) {
  117.                 $success = $db->insert($value_pairs);
  118.             }
  119.             //update
  120.             else {
  121.                 $success = $db->conditional_update($value_pairs, array(array( 'news_item_id','=',$this->db_id)));
  122.             }
  123.  
  124.           }
  125.  
  126.           public function get_date($style=1) {
  127.               if ($style == 1)
  128.                 return substr($this->creation_date, 0,10);
  129.               else if ($style == 2) {
  130.                   return substr($this->creation_date,9,1) . '/' . substr($this->creation_date,6,2) . substr($this->creation_date, 0,4);
  131.               }
  132.               else if ($style == 3) {
  133.                 return substr($this->creation_date,9,1) . ' ' . self::get_month_name(substr($this->creation_date,6,2)) . ' ' . substr($this->creation_date, 0,4);
  134.               }
  135.               else if ($style == 4) {
  136.                 return substr($this->creation_date,9,1) . ' ' . self::get_month_name(substr($this->creation_date,6,2),true) . ' ' . substr($this->creation_date, 0,4);
  137.               }
  138.  
  139.           }
  140.  
  141.           public static function get_month_name($nbr, $full_name= false) {
  142.               if (!$full_name)
  143.                 $month_names = array('jan','feb','mar','apr','maj','jun','jul','aug','sep','okt','nov','dec');
  144.               else
  145.                 $month_names = array('januari','februari','mars','april','maj','juni','juli','augusti','september','oktober','november','december');
  146.               return $month_names[$nbr-1];
  147.         }
  148.  
  149.         public function present_message($max_length = 0) {
  150.             if ( $max_length == 0)
  151.                 return $this->message;
  152.  
  153.             $disallowed_end_arr = array(',','.','!','?','-',';',':'."'");
  154.             if ( strlen($this->message) > $max_length ) {
  155.  
  156.                 for($i = $max_length; $i > 0; $i--) {
  157.                     if ( strcmp(substr($this->message,$i,1),' ') === 0 )
  158.                         if ( strcmp(substr($this->message,($i-1),1),',' ) === 0 ) {
  159.                             return substr($this->message,0,$i-1) . '...';
  160.                         }
  161.                         else
  162.                             return substr($this->message,0,$i) . '...';
  163.                 }
  164.             }
  165.             else
  166.                 return $this->message;
  167.         }
  168.  
  169.  
  170.           public function render($max_length = 0) {
  171.  
  172.                 if ( $this->use_lightbox_edit)
  173.                     $class = 'lbOn';
  174.                 else
  175.                     $class = '';
  176.                 $edit_link = '<a class="' . $class . '" href="adm_edit_news_item.php?ni=' . $this->db_id . '">Redigera</a>';
  177.                 $code = array();
  178.  
  179.                 $code[] = '<div class="news_item" style="border:1px solid #000;margin:10px 3px;padding:0;">' ."\n";
  180.                 $code[] = '<div class="news_item_header" style="background-color:#000;border-bottom:1px solid #000;">';
  181.                 $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>";
  182.                 $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>';
  183.                 if ($this->editable)
  184.                     $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 . '';
  185.  
  186.                 $code[] = "</div>";
  187.                 $code[] = '<div class="news_text" style="background-color:#eee;padding: 3px 5px;">' . $this->present_message($max_length);
  188.                 //$code[] = '<div class="news_text" style="background-color:#f00;padding: 3px 5px; color:#fff;">' . $max_length;
  189.                 if ($max_length != 0 && strlen($this->message) > $max_length) {
  190.                     if ($this->use_lightbox_view)
  191.                             $class = 'lbOn';
  192.                     else
  193.                         $class = '';
  194.                     $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>';
  195.                 }
  196.                 $code[] = "</div>";
  197.                 $code[] = "</div>";
  198.  
  199.                 return join("\n", $code);
  200.           }
  201.  
  202.           public function render_thumbnail_image() {
  203.  
  204.           }
  205.  
  206.       }
  207. ?>
  208.  
Best regards
Rythmic
Feb 24 '10 #1

✓ answered by Dormilich

see CNP: Headers already sent (Bytes Articles)

Share this Question
Share on Google+
2 Replies


Dormilich
Expert Mod 5K+
P: 8,639
see CNP: Headers already sent (Bytes Articles)
Feb 24 '10 #2

P: 29
Killer article. Thanks Dormilich and Markus

It was one of the more rare causes, an accidental whitespace before the <?php opening tag.
Feb 24 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.