473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why is this code trying to send header information

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)

2 1671
Dormilich
8,658 Expert Mod 8TB
see CNP: Headers already sent (Bytes Articles)
Feb 24 '10 #2
rythmic
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

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

Similar topics

3
by: dan glenn | last post by:
I have a php page which serves up multiple pages based on how the user interacts with it - there are links on the first page that will reload (from the same php file) a new page with form fields...
8
by: Pete Bennett | last post by:
Folks. I have some PHP code on my site at http://www.london-translations.co.uk/quick-quote.php Which allow people to get quotes for translation work. When the form is submitted control...
12
by: Fred Pacquier | last post by:
First off, sorry for this message-in-a-bottle-like post... I haven't been able to phrase my questions well enough to get a meaningful answer from Google in my research. OTOH, it is standard...
4
by: Adam Clauss | last post by:
OK, lets say I have a C# Windows application. In it is a a series of namespaces, all rooted for a certain namespace A. For ex, the "using" directives would read something like: using A; using...
3
by: Fredrik Melin | last post by:
Same problem that I had before, diffrent approach.. In my application at order-creation, the user might encounter that the item he is trying to order is locked by another user, and he have to...
2
by: mykyp | last post by:
Hi, I am trying to use my vb.net app as a replacement to a normal browser to interface with a website. I am trying to achieve 2 things: 1) logon to the website 2) complete forms and submit data...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
19
by: lawrence k | last post by:
How can I find out where my script is outputting to the screen for the first time? My error logs are full of stuff like this: PHP Warning: session_start(): Cannot send session cache...
16
by: matt | last post by:
I have used some free code for listing files for download, but I want to send an email to the administrator when the file has been downloaded. I have got some code in here that does it, but it will...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.