PHP無法在$_SERVER內取得自訂header
最近測試新版PHP時,有人提到他的程式在新的環境執行時無法取得自訂的header。可是,在舊環境執行卻可正常取得該header。 程式是採用$_SERVER取得header。原以為是新版PHP的問題,但查了PHP的版本變更記錄( PHP: Appendices - Manual )卻沒有相關的說明。於是,懷疑和apache有關,傳給PHP的資料就有問題。 此次,除了PHP升級外,Apache也一併升級到2.4。在官方說明- Overview of new features in Apache HTTP Server 2.4 - Apache HTTP Server Version 2. 中提到,Apache 2.4為了增加安全做了以下調整… Translation of headers to environment variables is more strict than before to mitigate some possible cross-site-scripting attacks via header injection. Headers containing invalid characters (including underscores) are now silently dropped. Environment Variables in Apache has some pointers on how to work around broken legacy clients which require such headers. (This affects all modules which use these environment variables.) 依據上述資訊,重新查了有問題的程式。原來,程式中所自訂的header名稱中包含了『_』(底線、underscores)。觸發了Apache 2.4上述的安全加強,PHP就無法在$_SERVER取得header名稱中有底線『_』的header。當然,自訂的header名稱中沒有使用底線『_』的header,是可以正常取得的。 以下,簡單說明問題發生的原因、與相關的狀況,與處理方式…