Skip to content

Instantly share code, notes, and snippets.

@cdaven
Last active December 15, 2015 00:09
Show Gist options
  • Save cdaven/5170581 to your computer and use it in GitHub Desktop.
Save cdaven/5170581 to your computer and use it in GitHub Desktop.
A small PHP function to log a message to the HTTP header(s) X-Log-Message.
function __log2header($message)
{
if (headers_sent())
{
return false;
}
// Escape newline (not allowed in headers)
$message = str_replace("\n", "\\n", $message);
if(mb_strlen($message) > 8000)
{
// Cut long messages after 8000 characters
// (since headers shouldn't be much longer than that)
$message = mb_substr($message, 0, 8000).'...';
}
header("X-Log-Message: $message", false);
return true;
}
@cdaven
Copy link
Author

cdaven commented Mar 18, 2013

It seems current versions of Google Chrome and Internet Explorer show header values in UTF-8, while Firefox shows them in ISO 8859-1. This function assumes UTF-8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment