james david low

live / work / play / worship

HTTP headers and raw POST data for PHP-CGI

I host my website on Dreamhost and as good value as it is, with great email/SVN/MySQL and hosting, there are some limitations. One of them is trying to read raw POST data from $HTTP_RAW_POST_DATA and the other is trying to read custom HTTP headers because PHP runs as a CGI module using there service.

I realised the working around for getting raw post data is to use:

$data = file_get_contents(‘php://input’);

And that you can use mod_rewrite to to pass HTTP headers to the $_SERVER variable, as described here. If you want to add additional header values, you must specify each one as a seperate E= statement in your .htaccess file. I was writing a CALDAV server and wanted to be able to (1) use the HTTP authentication described here, (2) read the “Depth” HTTP header, and (3) redirect all requests queries through a single php file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [E=REDIRECT_REMOTE_USER:%{HTTP:Authorization},E=DEPTH:%{HTTP:Depth},L]
RewriteRule .* – [E=REDIRECT_REMOTE_USER:%{HTTP:Authorization},E=DEPTH:%{HTTP:Depth},L]

If you found any of the software useful, please consider supporting its further developement by donating.
10:08pm / Nov 10th / 09
« Mail.app RSS Smart Mailbox Thoughts On Keyboards »