PHP getallheaders無法擷取自訂頭(headers)的問題,phpgetallheaders_PHP教程

來源:互聯網
上載者:User

PHP getallheaders無法擷取自訂頭(headers)的問題,phpgetallheaders


在用戶端請求的時候增加了自訂的http頭,請求如下所示:

自訂http要求標頭

var_dump(getallheaders);

一開始通過getallheaders參數擷取,但是發現在nginx部署的伺服器上擷取不到,非常奇怪,查看php手冊發現getallheaders這個函數只支援apache伺服器。於是找到相容的方法:

if (!function_exists('getallheaders')) {function getallheaders() {$headers = array();foreach ($_SERVER as $name => $value) {if (substr($name, 0, 5) == 'HTTP_') {$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;}}return $headers;}}var_dump(getallheaders());

實際上這個方法就是找到$_SERVER變數中以HTTP_開頭的屬性,對屬性做一個字串替換這樣。$_SERVER變數中的HTTP_USER_ID實際就是上面自訂的User-Id:

php中$_SERVER變數

另外關於自訂Http頭, 需要注意頭的命名規範,不能用底線命名 ,否則在nginx伺服器下讀取不到,在尋找命名規範的時候,有提到自訂屬性用X-開頭這個問題。後來查閱了一些資料,發現後來的http協議不建議這樣去做。

以上內容是針對PHP getallheaders無法擷取自訂頭(headers)的問題的相關敘述,希望對大家有所協助!

您可能感興趣的文章:

  • php session_start()關於Cannot send session cache limiter - headers already sent錯誤解決方案
  • php 類比get_headers函數的程式碼範例
  • 使用php get_headers 判斷URL是否有效解決辦法
  • php中get_headers函數的作用及用法的詳細介紹
  • PHP提示Cannot modify header information - headers already sent by解決方案
  • PHP錯誤Warning: Cannot modify header information - headers already sent by解決方案
  • PHP使用get_headers函數判斷遠程檔案是否存在的方法
  • PHP實現的帶逾時功能get_headers函數

http://www.bkjia.com/PHPjc/1113707.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1113707.htmlTechArticlePHP getallheaders無法擷取自訂頭(headers)的問題,phpgetallheaders 在用戶端請求的時候增加了自訂的http頭,請求如下所示: 自訂http要求標頭...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.