0

I have created a graph using php. The script is working correctly when it is run separately, but when I put it with other code, instead of a graph some strange characters show, the error is :

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\samplesite\header.php:99) in C:\xampp\htdocs\samplesite\Pets\Dogs\Afgan Hound.php on line 161

header.php has been included at the start of page.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
ashish
  • 49
  • 1
  • 7
  • If you are trying to change the headers ie. using `header` you need to make sure that no output has been sent. – GWW Aug 20 '11 at 02:17

1 Answers1

0

Your header.php script sends some output, possibly even just whitespace before headers are called in your main script. If your AfganHound.php script attempts to store a cookie via setcookie(), or calls session_start() after header.php has been included, the action will fail with the error you received.

If header.php produces output (and we suspect it does), it must be included after your main script sets cookies or calls session_start()

According to comments, the main script calls header("Content-type:image/png"); to output an image.

This is the source of the problem. You cannot output both HTML and an image in the same script execution. Your HTML page (the one with the menus created by header.php should contain an <img> tag which calls the graphing script as its src attribute. When the browser loads the HTML page and requests this <img>, instead of being served a static png image, it calls the PHP script which produces the graph and delivers it back to the browser as an image. The browser is unaware that it has called a PHP script, and just treats it like any other image to display.

<img src='/path/to/graph_generator_script.php' alt='what the image contains' />

Then the graph generation script must not produce any output except for the image data (no HTML), with its associated header:

header("Content-type:image/png"); 
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • header.php contains menu of the website so i have and then fetch some data from DB and then used the script to create graph and at the end of graph script it has header("Content-type:image/png"); – ashish Aug 20 '11 at 02:22
  • I am able to understand what u r saying but only lil bit. I have not put the graph script in separate file , it is same file where i call header.php – ashish Aug 20 '11 at 02:30
  • @ashish That is your problem. You cannot produce an image in the same script that produces any HTML. The graph png image is not HTML data, and cannot be served by the same script that produces the page on which it is displayed. An image must be produced in a separate script all by itself. – Michael Berkowski Aug 20 '11 at 02:33
  • Could u please let me know how can i call the other file that has graph script , as when i try to call that file it still gives header error – ashish Aug 20 '11 at 03:22
  • @ashish remove the `include(header.php)` from the graph script. The graph script should only display the png image, nothing else. Then use the `` as above, which will actually show the graph in an HTML page. If you just call the graph script directly from the browser, you should see only an image. – Michael Berkowski Aug 20 '11 at 03:25