Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Content-type; problem

Status
Not open for further replies.

gnubie

Programmer
Apr 16, 2002
103
0
0
US
I have a Perl script that might print text/html at various places, so near the beginning I put:

print "Content-type: text/html\n\n";

This works fine, EXCEPT, and the end in some cases the script redirects to another web page. The problem is that if the above Content-type has been output, the redirect:

print "Location: "../thankyou.htm\n\n";

just prints out.

SO, is there a way to override the Content-type and make it redirect. Any other good workaround?

Thanks,

GN
 
only print the content-type header if and when needed instead of at the beginning of the script.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
KevinADC:

First, I was hoping to avoid preceding each print with a print "Content-type: text/html\n\n"; or creating a scheme to keep track of it in the Perl script. Otherwise, after outputting the first print "Content-type:", the remaining ones would display the text "Content-type:", now requiring more conditional code.

I was hoping for an elegant solution like:

1. A system query or way to detect if the server was already expecting text/html, or
2. A way to explicitly override or change it.

If there is no elegant solution, I can cobble together enough code to get around it.
 
There is no solution. Once the header is printed there is no retracting it, so you have to print it conditionally as needed to avoid the situation you find yourself in.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Javascript redirect

print "<SCRIPT LANGUAGE=JavaScript><!--
setTimeout(location.href = '../thankyou.htm\n\n',5000);
//--></SCRIPT>";

5000 is 5 seconds I do believe... You can print it anywhere after the <html> tag I do believe

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
You have at least two other methods, besides the one described by travs69:
-do not close the header in the line
[tt]print "Content-type: text/html\n\n";[/tt]
by adding a single eol instead of two; you'll print the closing eol just before starting with html content
-when you need a redirect print a short html page with the tag [tt]<META HTTP-EQUIV="Refresh" CONTENT="5; URL=html-redirect.html">[/tt] in the head section: 5 is a delay in seconds (may be zero). I think that this method is now deprecated though.

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
travs69 & prex1: Thanks. Good to know. I will add them to bag of tricks. I found a third method.

I ended up adding an unconditional print "Content-type: ..." at the beginning of the script, then INSTEAD of a redirect (which would have failed) I opened the file I would have redirected to, slurped it in, and printed it.

(open + slurp + print) ~ redirect

GN
 
You have at least two other methods, besides the one described by travs69:
-do not close the header in the line
print "Content-type: text/html\n\n";
by adding a single eol instead of two; you'll print the closing eol just before starting with html content

Yes, but you still have to manually track when to finish printing the header. There is really little or no benefit from this approach. Just print the heades when they are needed and not before. There is nothing wrong with having:

print "Content-type: blah/blah\n\n";

several times in a script.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Well, I'd go with Kevin and say only print the content type header when/if you need it. If you structure your program carefully it should be straightforward enough.

However, if you really have to have bits of printing all over the place and no idea whether you've done a header, just use a subroutine:
Code:
# Somewhere near the top of the program, initialise a flag
$HEADER_DONE = 0;

... whatever code here ...

&head_if_reqd;
print "hello world";

... more code here ...

&head_if_reqd;
print "hello world again";

... etc. ...

sub head_if_reqd {
   unless ($HEADER_DONE) {
      print "Content-type: text/html\n\n";
   }
   $HEADER_DONE = 1;
}


-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top