Page 1 of 1
Modify HTTP-Header in PHP running as CGI
Posted: Fri Sep 10, 2004 2:28 pm
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
Posted: Fri Sep 10, 2004 2:49 pm
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.
Posted: Fri Sep 10, 2004 3:02 pm
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"?
Posted: Fri Sep 10, 2004 3:03 pm
by znum
Posted: Fri Sep 10, 2004 3:13 pm
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>
Posted: Fri Sep 10, 2004 3:22 pm
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
Posted: Fri Sep 10, 2004 3:25 pm
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.
Posted: Fri Sep 10, 2004 3:27 pm
by feyd
text/plain

Posted: Fri Sep 10, 2004 3:29 pm
by znum
no change

Posted: Fri Sep 10, 2004 3:31 pm
by znum
could it be a problem that the cgi-server is using https instead of http?
Posted: Fri Sep 10, 2004 3:37 pm
by redmonkey
feyd wrote:text/plain

Thanks, I'm on wind-down mode tonight so the mind was drawing a blank
