Modify HTTP-Header in PHP running as CGI

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
znum
Forum Newbie
Posts: 6
Joined: Fri Sep 10, 2004 2:20 pm

Modify HTTP-Header in PHP running as CGI

Post by znum »

Is it possible to use the header-Function to modify the HTTP-header with PHP running as CGI?

So far I think it fails for me because my apache-webserver runs PHP as CGI and I have to use #!/usr/bin/php in the first line. And so (i think) the header-modification will not work because there is already a output to the file.

Any idears on this?

thx
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I believe the shebang is stripped out of the file before php gets ahold of it.. otherwise, you'd see it.

why not try it? or maybe you are modifying the header wrong? post your code.
znum
Forum Newbie
Posts: 6
Joined: Fri Sep 10, 2004 2:20 pm

Post by znum »

Code: Select all

#!/usr/bin/php 
<?php ob_start();
header("Content-type: text/html"); 
header("Location: http://server/goto.php");
?>
The output: http://www.tfh-berlin.de/~s19436/loc.php

What is the "shebang"?
znum
Forum Newbie
Posts: 6
Joined: Fri Sep 10, 2004 2:20 pm

Post by znum »

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

#! = shebang or sharp bang.

headers:

Code: Select all

HTTP/1.1 302 Found
Date: Fri, 10 Sep 2004 20:12:23 GMT
Server: Apache/1.3.27 (Unix)  (Red-Hat/Linux) mod_ssl/2.8.12 OpenSSL/0.9.6b DAV/1.0.2 mod_perl/1.26
Location: https://cgi.tfh-berlin.de/~s19436/loc.php
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1
content:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>302 Found</TITLE>
</HEAD><BODY>
<H1>Found</H1>
The document has moved <A HREF="https://cgi.tfh-berlin.de/~s19436/loc.php">here</A>.<P>
<HR>
<ADDRESS>Apache/1.3.27 Server at www.tfh-berlin.de Port 80</ADDRESS>
</BODY></HTML>
znum
Forum Newbie
Posts: 6
Joined: Fri Sep 10, 2004 2:20 pm

Post by znum »

Okay. but this is only the Apache-Redirection to "https://cgi.tfh-berlin.de/~s19436/loc.php"...

and there:

headers:

Code: Select all

Date: Fri, 10 Sep 2004 20:22:06 GMT
Server: Apache/1.3.26 (Linux/SuSE) mod_perl/1.27 mod_ssl/2.8.10 OpenSSL/0.9.6g
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html
content:

Code: Select all

Content-type: text/html Location: http://server/goto.php
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Headers ideally should be sent prior to any other output to the buffer (although I believe you can get round this using ob_start).

The shebang line is an instruction to the server in order to locate the appropriate interpreter for the script, and as a result is not included (or classed) as output to the buffer therefore you should have no problems (I know I don't) modifying the headers when running the CGI version of PHP.

It is worth noting, if no headers are set in the script then the defaults are applied which in the case of CGI-PHP is text/html. So line 3 of script does not really show if the headers have been succesfully changed. Perhaps try changing the 'Content-type' to 'text/plain' (or is it 'plain/text' can quite remember at the moment) to ascertain of you can modify the headers.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

text/plain

;)
znum
Forum Newbie
Posts: 6
Joined: Fri Sep 10, 2004 2:20 pm

Post by znum »

no change :(
znum
Forum Newbie
Posts: 6
Joined: Fri Sep 10, 2004 2:20 pm

Post by znum »

could it be a problem that the cgi-server is using https instead of http?
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

feyd wrote:text/plain

;)
Thanks, I'm on wind-down mode tonight so the mind was drawing a blank :)
Post Reply