Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does header() work?

Tags:

php

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 includes 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.

like image 689
Niet the Dark Absol Avatar asked Nov 10 '11 15:11

Niet the Dark Absol


2 Answers

The header() function is totally ignored by the CLI SAPI. It has an effect on the Apache and CGI SAPIs though.

Simply 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!
like image 200
netcoder Avatar answered Sep 27 '22 18:09

netcoder


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.

like image 41
Alessandro Desantis Avatar answered Sep 27 '22 18:09

Alessandro Desantis