How does header () work?

A little context:
I needed to run PHP scripts in a browser, but I did not want to go for all the problems with installing the server and suffer from the overhead of running the server on my computer and everything connected with it, including firewalls, blah blah blah.

So instead, I wrote my own server. This is a simple PHP script that listens for connections on port 80 of my LAN-IP, then I just load that IP address into my browser and it works. It receives an HTTP request and runs a second PHP script using exec - this means that I can easily make changes to it without restarting the script server. This second PHP script parses the request and finally include called script. It gets the output from there and sends the response back to the browser with the appropriate headers (which I can change).

Yes, this is a mess, but it works. He does what I need.

Now to the question:
I can not use header() . This does not seem to affect what is sent back to the browser through the socket connection. Instead, I made the setheader() function and saved the headers in an array to be added to the answer.

I would like to know how the header() function really works inside, so I can use this function instead of my "hacked" one.

+7
source share
7 answers

The header() function is completely ignored by the SAPI CLI. However, this affects SAPI in Apache and CGI.

Simply put, the CLI SAPI does not implement any logic in the sapi_*_header_* functions. For 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 of these functions basically return NULL , 0 or a false success message.

For 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 do this work using binary, etc. php-cgi arrays:

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! 
+2
source

The header() function works exactly the same as your implementation: it simply sends text headers in the response before the content of the page (therefore, it will cause an error if any content has already been sent to the client). So, I think you are doing it right. Anyway, you have some problems in your implementation. I think installing a web server would be a lot easier.

+1
source

And do you use the correct CGI style call? - otherwise, most HTTP related functions will not work in PHP, and CGI is no better than calling a library from Apache or IIS

Why don't I just take EasyPHP or something like that?

PS What do you mean by "query parsing"?

0
source

Actual sender headers are performed using the SAPI API Level (API Server). Examples of SAPI include mod_php for Apache and the CLI version of PHP that will be launched when exec called. In the case of the SAPI CLI, sending headers is noop, since it usually doesn't make sense to display the HTTP headers for the script on the command line.

Try using php-cgi as binary instead of PHP. CGI SAPI must display the correct headers.

0
source

It is up to the SAPI module (the code that calls the PHP script) to decide what to do with the headers. The SAPI CLI you are probably using just ignores any headers set by the script. CGI SAPI, which you can use with the php-cgi command, displays them by default, but supports a command line interface to suppress them. Ideally, you just write your own SAPI module that interacts correctly with your web server.

0
source

A typical HTTP response is as follows:

 HTTP/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 header() function takes care of putting - well, - headers in the first part of the response. You can output actual HTML using print , etc.

Now, if this is a PHP script, playing in Apache, this script is responsible for entering your header lines. header() only works if the output is sent to stdout, and not to the user written to the socket, so that after the completion of the PHP-CGI script, the corresponding headers can be added to the output.

0
source

I know that I will seem stupid, but why not just install a lamp or a lamp? It really "unfastens and starts and voila", which really will save you from trouble.

0
source

All Articles