A little context:
I needed to run PHP scripts in a browser, but I didn't want to go to all the trouble of installing a server and suffer the overhead of running a server on my computer and all the stuff that goes with it, including firewalls, blah blah blah.
So instead I wrote my own server. It's a simple PHP script that listens for connections on port 80 of my LAN IP, then I just load that IP in my browser and it works. It receives the HTTP request and starts a second PHP script using exec
- this is so that I can make changes to it easily without having to restart the server script. This second PHP script parses the request, and finally include
s the script that was actually being called. It gets the output from there, and sends the response back to the browser with appropriate headers (which I can change).
Yeah, it's a mess, but it works. It does what I need it to do.
Now for the question:
I can't use header()
. It doesn't seem to be having any effect on what gets sent back to the browser through the socket connection. I have instead made a setheader()
function, and store headers in an array to be prepended to the response.
I'd like to know how the header()
function actually works internally, so that I might be able to use that function instead of my "hacked" one.
Source: Tips4all, CCNA FINAL EXAM
The header() function is totally ignored by the CLI SAPI. It has an effect on the Apache and CGI SAPIs though.
ReplyDeleteSimply put, the CLI SAPI doesn't implement any logic in the sapi_*_header_* functions. Per example, for the CLI SAPI, in php_cli.c:
static int sapi_cli_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) /* {{{ */
{
/* We do nothing here, this function is needed to prevent that the fallback
* header handling is called. */
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
/* }}} */
All those functions basically return NULL, 0 or a fake success message.
For the CGI SAPI, in cgi_main.c:
static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
{
char buf[SAPI_CGI_MAX_HEADER_LENGTH];
sapi_header_struct *h;
zend_llist_position pos;
zend_bool ignore_status = 0;
int response_status = SG(sapi_headers).http_response_code;
// Lots of other code...
}
You can easily make this work using the php-cgi binary and some array manipulation:
server.php
$script_to_run = 'script.php';
exec('php-cgi '.escapeshellarg($script_to_run), $output);
$separator = array_search("", $output);
$headers = array_slice($output, 0, $separator);
$body = implode("\r\n", array_slice($output, $separator+1));
print_r($headers);
print $body;
script.php
header('Content-Type: text/plain');
echo 'Hello, World!';
Output:
Array
(
[0] => X-Powered-By: PHP/5.3.8
[1] => Content-Type: text/plain
)
Hello, World!
The header() function works exactly as your implementation does: it just sends plain text headers in the response, before the page's content (that's why it will throw an error if any content has been already sent to the client). So, I think you're doing it the right way. Anyway, you have some problems in your implementation. I think installing a Web server would have been a lot easier.
ReplyDeleteAnd do you use correct CGI-style calling ? - otherwise most HTTP related functions will not work in PHP - and still CGI is not as best as library call from Apache or IIS
ReplyDeleteWhy just don't take EasyPHP or something like this ?
PS What do you mean by "parsing request" ?
The actual sending headers is performed by the SAPI (server API) layer of PHP. Example SAPIs include mod_php for Apache and the CLI version of PHP which will be run when you call exec. In the case of the CLI SAPI, sending headers is a noop since it typically doesn't make sense to display HTTP headers for a script run over the command line.
ReplyDeleteTry using php-cgi as the binary instead of PHP. The CGI SAPI should output the correct headers.
It's up to the SAPI module (the code that invokes the PHP script) to decide what to do with the headers. The CLI SAPI, which you're probably using, just ignores any headers set by the script. The CGI SAPI, which you could use with the php-cgi command, outputs them by default but supports a command line interface to suppress them. Ideally, you would just write your own SAPI module that interfaces properly with your web server.
ReplyDeleteA usual HTTP response looks like that:
ReplyDeleteHTTP/1.1 200 OK
Date: Thu, 10 Nov 2011 15:35:27 GMT
Server: Apache
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=None
<!DOCTYPE html>
<html>
....
The PHP function header() takes care of putting - well, - headers into the first part of the response. The actual HTML you can output using print and the likes.
Now, if it's a PHP script, playing Apache, this script is responsible for putting your header lines in its response. header() only works if the output is sent to stdout and not custom written to a socket so that, after a PHP-CGI script ends, appropriate headers can be prepended to the output.
I know I'm gonna sound stupid, but why not just install wamp or lamp ? It's a really "unzip & run & voilà " type of solution, that would really spare your the trouble.
ReplyDelete