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.
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, inphp_cli.c:All those functions basically return
NULL,0or a fake success message.For the CGI SAPI, in
cgi_main.c:You can easily make this work using the
php-cgibinary and some array manipulation:server.php
script.php
Output: