Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a situation, which I had solved quite some time ago. Unfortunately I can not recall the solution or find the old code. So here goes:

I have a script which needs to output both html and graphics on the same page. I am using the GD::Graph module to create the graphic. In order to display everything with the same script, it appears I will need to send two content-type headers ( text/html and image/png ). How do I do this within my cgi script?

Replies are listed 'Best First'.
Re: Sending multiple http headers
by Beatnik (Parson) on Aug 22, 2001 at 19:45 UTC
    You can't set multiple HTTP headers in one call. Once a certain content-type is set in the HTTP header, you're stuck with it. You can however generate different data & content-types depending on certain parameters... check Re: Displaying randomly created png's in HTML for more specifics and a link to the correct RFC.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
      These are two separate web server requests. The first one fetches the html document with the IMG SRC=/cgi-bin/images/whatever.png. This content type should be set to text/html. The web browser will make a separate request for whatever.png. This will be a separate cgi script that just sets the content type to image/png (i think) and basically prints the binary image file.
        "I have a script which needs to output both html and graphics on the same page. "
        and
        "..., it appears I will need to send two content-type headers ( text/html and image/png )."

        The question was kinda tricky. My response did include the two request solution.

        I doubt Cines answer below actually works on web browsers. MIME types and HTTP content-types are similar but not exactly the same.

        Greetz
        Beatnik
        ... Quidquid perl dictum sit, altum viditur.
Re: Sending multiple http headers
by Cine (Friar) on Aug 22, 2001 at 20:41 UTC
    Take a look at the bugzilla 2.12 package's buglist.cgi line 887-, which does almost what you are asking.

    It basicly comes down to that the first header you send you tell that what you are sending is a multipart message, where the first is your html and the second is your png picture.

    T I M T O W T D I