PHP header problems.

Discussion in 'PHP' started by Nick_Mayhem, May 9, 2006.

  1. #1
    Well I having headaches from last 3 days. So finally decided to post it here.

    I have made a page in which there is some content.

    <?php
    echo $content;
    
    //images code
    //Now the dynamically created graph
    print_image();
    ?>
    PHP:
    The above thing is a simplified version of my script.

    Now what happens is that whenever I test the code without echo on the top line then the graph which is dynamically created is shown.

    But my website needs the content on the top and not after the image.

    The problem is with the header() thing.

    I had mentioned header ("Content-Type: image/png");

    But the headers are already sent so is there a way to modify or send new headers in that same page?

    Please let me know. Thanks.
     
    Nick_Mayhem, May 9, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think you would be better keeping the content and the image in seperate scripts.

    <?php
    echo "content";
    
    include("imagescript.php");
    
    ?>
    PHP:
    Or if that fails just echo the image using <img src="imagescript.php">
     
    mad4, May 9, 2006 IP
  3. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #3
    AFAIK one page can only have one header, else how does the browser know how to interpret it?

    Looks like the print_image is meant to output a picture on its own (separate page), not inline amongst content. Seems to me you will have to alter that function - code a variation that just echoes the image html, no headers.
     
    T0PS3O, May 9, 2006 IP
  4. Nick_Mayhem

    Nick_Mayhem Notable Member

    Messages:
    3,486
    Likes Received:
    338
    Best Answers:
    0
    Trophy Points:
    290
    #4
    I tried the code in other page too. But then it shows me the binary data of the PNG as a text instead of PNG image.
     
    Nick_Mayhem, May 9, 2006 IP
  5. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Can you post the image function's content?
     
    T0PS3O, May 9, 2006 IP
  6. Nick_Mayhem

    Nick_Mayhem Notable Member

    Messages:
    3,486
    Likes Received:
    338
    Best Answers:
    0
    Trophy Points:
    290
    #6
    Nick_Mayhem, May 9, 2006 IP
  7. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Looking at the code I am pretty sure that you can just include it as I posted above.
     
    mad4, May 9, 2006 IP
  8. Nick_Mayhem

    Nick_Mayhem Notable Member

    Messages:
    3,486
    Likes Received:
    338
    Best Answers:
    0
    Trophy Points:
    290
    #8
    In short I am in a Mess :eek:
     
    Nick_Mayhem, May 9, 2006 IP
  9. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I agree with mad4: have you tried using it as an 'img src'?

    HTML is HTML, no matter how you generate it. When doing things like this you have to work out how the HTML is going to look in the end. Have you ever seen an image with its actual 'binary source' embedded in the HTML? No. You have the HTML use the image tag with the src attribute.

    This doesn't (can't!) change, just because you're using PHP. You need to have your content in HTML which also creates an image link back to the output of your print_image function.
     
    TwistMyArm, May 9, 2006 IP
  10. Nick_Mayhem

    Nick_Mayhem Notable Member

    Messages:
    3,486
    Likes Received:
    338
    Best Answers:
    0
    Trophy Points:
    290
    #10
    Yes TwistmyArm I checked it out. And I am a developer from last 2 years. I know that basics. But what my problem is that this kind of images can be generated dynamically Bcoz it is graph. In example also the graph is showing but When I even echo "HI" above (Means before the image) PHP sends out default headers and it prevents my dynamic image to be displayed properly (Or can be said not display it at all)
     
    Nick_Mayhem, May 9, 2006 IP
  11. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Try this.

    Put the <?php print_image(); ?> in a separate file and call it graph.php.

    Now under echo $content; just type:

    <img src="graph.php">

    The print_image function seems to send headers saying it is an image. Treating it as one could fix it.
     
    T0PS3O, May 9, 2006 IP
  12. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #12
    T0PS3O is exactly right and that's what both myself and mad4 have tried to say (just not as straight forward as that, I guess!).

    Nick_Mayhem: I appreciate that you may be a programmer but you're still confusing your outputs. Even if PHP didn't send a header before the string 'HI', you'd still be wanting PHP to send an image with a separate string that isn't part of the image before it. In effect, you would end up with the image data but with the letters H and I at the start of the image 'source'. Surely you can see that that effectively would create an invalid image anyway?

    Anyway, do exactly as T0PS3O says and you won't have a problem.
     
    TwistMyArm, May 9, 2006 IP
  13. Nick_Mayhem

    Nick_Mayhem Notable Member

    Messages:
    3,486
    Likes Received:
    338
    Best Answers:
    0
    Trophy Points:
    290
    #13
    WOWO DP rocks. I figured it out. :D

    it took 13 different PHP files to work it around. But at last it is done. :D :D HE EHEH. I am so happy.
     
    Nick_Mayhem, May 9, 2006 IP
  14. DrQuincy

    DrQuincy Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Listen to TwistMyArm. A file can only be one type ergo only one content-type header() can be sent!
     
    DrQuincy, May 10, 2006 IP
  15. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Nick_Mayhem:

    Again, not knowing your exact situation, but don't forget that you can still pass in parameters to the image code via GET. If your 13 scripts are 'pretty close' to each other, you might want to look at doing that.

    Something along the lines of:
    <img src="/path/to/script.php?param=x&other_param=y">

    for example. That way, you can keep the common code and just use switch / ifs for the non-common stuff.

    Glad to hear you've got it working though!
     
    TwistMyArm, May 10, 2006 IP
  16. Nick_Mayhem

    Nick_Mayhem Notable Member

    Messages:
    3,486
    Likes Received:
    338
    Best Answers:
    0
    Trophy Points:
    290
    #16
    :D Thanks. The code was much complicated then explained above. It needed some parameters like graph values, title, yaxiz ticks, x axis numbers or labels, transperent color, background color, bar color, numbers and title color, my copyright notice to be printed on it. Etc etc.

    But it was fun doing it at last. And thanks to all of you for your help and suggestions. Thanks a lot.
     
    Nick_Mayhem, May 10, 2006 IP
  17. tech86

    tech86 Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #17
    its not possible to put image drawing code and text on the same page due to context-type

    simple enough do this

    <?php
    echo "content";
    echo "<img src=\"draw_image.php?params=1gs\">";
    ?>
     
    tech86, May 11, 2006 IP