Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Web Headers

by Anonymous Monk
on Oct 31, 2002 at 18:49 UTC ( [id://209478]=perlquestion: print w/replies, xml ) Need Help??

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

I have several perl functions that are running on a web server that print the web browser. Is there a way to determine whether a web browser has already been sent the web headers? A very simple example of code that I am using follows.

The problem is that when the headers get printed twice, this appears: "Content-Type: text/html; charset=ISO-8859-1". Ideally this check would take place in the "send_headers" function. The way I am doing it now is a sloppy, sloppy hack.
sub my_func { my($check) = @_; if ($check eq "no") { &send_headers; } #do stuff here return(); } sub send_headers { my $query = CGI:new(); print $query->header; }

Replies are listed 'Best First'.
Re: Web Headers
by Ovid (Cardinal) on Oct 31, 2002 at 18:56 UTC

    Instantiate one and only one CGI object and you can ensure that you have unique headers in a couple of different ways.

    use CGI qw/:unique_headers/; # or use CGI; $CGI::HEADERS_ONCE = 1; my $query = CGI->new();

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Is this documented anywhere? I see it in the CGI.pm file, but not in it's documentation anywhere?

      -- Dan

        Sure it's documented. Just look at the node you replied to :)

        Seriously, I don't know where this is documented. It's just one of those things that you learn after a while. It might even be a debugging tool because frankly, if you can't control when or where your headers are being printed, this means there's a bug somewhere. Documenting features to compensate for buggy code only seems (to me) to encourage such buggy code. However, I do feel that it should be documented with at least a warning that it shouldn't be used unless absolutely necessary.

        Cheers,
        Ovid

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Web Headers
by John M. Dlugosz (Monsignor) on Oct 31, 2002 at 23:13 UTC
    print $query->header unless $already_did_that++;
Re: Web Headers
by kshay (Beadle) on Nov 01, 2002 at 19:13 UTC
    Why would you need to send the header from within a subroutine that gets called more than once? You're always going to want to return something to the browser no matter what happens, and you're never going to want more than one instance of the header, so why not just print it once within your main script?

    --Kevin

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://209478]
Approved by Ovid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found