Last active
December 15, 2015 00:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.