accept标头 php,如何在PHP中读取任何请求标头
如何在PHP中讀取任何請求標頭
我應該如何閱讀PHP中的任何標題?
例如,自定義標頭:X-Requested-With。
Sabya asked 2019-02-28T12:09:45Z
14個解決方案
349 votes
$_SERVER['HTTP_X_REQUESTED_WITH']
RFC3875,4.1.18:
如果使用的協議是HTTP,則名稱以HTTP_開頭的元變量包含從客戶端請求標頭字段讀取的值。 HTTP標頭字段名稱轉換為大寫,所有出現的-都替換為_,并且前綴為HTTP_以提供元變量名稱。
Quassnoi answered 2019-02-28T12:11:14Z
256 votes
IF:您只需要一個標題,而不是所有標題,最快的方法是:
// Replace XXXXXX_XXXX with the name of the header you need in UPPERCASE (and with '-' replaced by '_')
$headerStringValue = $_SERVER['HTTP_XXXXXX_XXXX'];
ELSE IF:您將PHP作為Apache模塊運行,或者從PHP 5.4開始,使用FastCGI(簡單方法):
apache_request_headers()
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value
\n";
}
ELSE:在任何其他情況下,您都可以使用(userland implementation):
function getRequestHeaders() {
$headers = array();
foreach($_SERVER as $key => $value) {
if (substr($key, 0, 5) <> 'HTTP_') {
continue;
}
$header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
$headers[$header] = $value;
}
return $headers;
}
$headers = getRequestHeaders();
foreach ($headers as $header => $value) {
echo "$header: $value
\n";
}
也可以看看:
getallheaders() - (PHP&gt; = 5.4)跨平臺版別名apache_request_headers()apache_response_headers() - 獲取所有HTTP響應頭。
headers_list() - 獲取要發送的標頭列表。
Jacco answered 2019-02-28T12:10:39Z
47 votes
您應該在$_SERVER全局變量中找到所有HTTP標頭,前綴為HTTP_大寫,短劃線( - )替換為下劃線(_)。
例如,您的$_SERVER可以在以下位置找到:
$_SERVER['HTTP_X_REQUESTED_WITH']
從$_SERVER變量創建關聯數組可能很方便。 這可以用幾種樣式完成,但這是一個輸出camelcased鍵的函數:
$headers = array();
foreach ($_SERVER as $key => $value) {
if (strpos($key, 'HTTP_') === 0) {
$headers[str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))))] = $value;
}
}
現在只需使用$_SERVER來檢索所需的標題。
PHP手冊$_SERVER:[http://php.net/manual/en/reserved.variables.server.php]
Thomas Jensen answered 2019-02-28T12:12:21Z
18 votes
從PHP 5.4.0開始,您可以使用getallheaders函數將所有請求的頭返回為關聯數組:
var_dump(getallheaders());
// array(8) {
// ? ["Accept"]=>
// ? string(63) "text/html[...]"
// ? ["Accept-Charset"]=>
// ? string(31) "ISSO-8859-1[...]"
// ? ["Accept-Encoding"]=>
// ? string(17) "gzip,deflate,sdch"
// ? ["Accept-Language"]=>
// ? string(14) "en-US,en;q=0.8"
// ? ["Cache-Control"]=>
// ? string(9) "max-age=0"
// ? ["Connection"]=>
// ? string(10) "keep-alive"
// ? ["Host"]=>
// ? string(9) "localhost"
// ? ["User-Agent"]=>
// ? string(108) "Mozilla/5.0 (Windows NT 6.1; WOW64) [...]"
// }
之前,此功能僅在PHP作為Apache / NSAPI模塊運行時才起作用。
Salman A answered 2019-02-28T12:13:07Z
6 votes
strtolower缺少若干提議的解決方案,RFC2616(HTTP / 1.1)將頭字段定義為不區分大小寫的實體。 整個事情,不僅僅是價值部分。
因此,僅解析HTTP_條目的建議是錯誤的。
更好的是這樣的:
if (!function_exists('getallheaders')) {
foreach ($_SERVER as $name => $value) {
/* RFC2616 (HTTP/1.1) defines header fields as case-insensitive entities. */
if (strtolower(substr($name, 0, 5)) == 'http_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
$this->request_headers = $headers;
} else {
$this->request_headers = getallheaders();
}
請注意與先前建議的細微差別。 這里的函數也適用于php-fpm(+ nginx)。
Glenn Plas answered 2019-02-28T12:14:14Z
5 votes
將標題名稱傳遞給此函數以獲取其值,而不使用for循環。 如果未找到標頭,則返回null。
/**
* @var string $headerName case insensitive header name
*
* @return string|null header value or null if not found
*/
function get_header($headerName)
{
$headers = getallheaders();
return isset($headerName) ? $headers[$headerName] : null;
}
注意:這僅適用于Apache服務器,請參閱:[http://php.net/manual/en/function.getallheaders.php]
注意:此函數將處理并將所有標頭加載到內存中,并且它的性能低于for循環。
Milap Kundalia answered 2019-02-28T12:15:23Z
3 votes
為了簡單起見,您可以通過以下方式獲得所需的內容:
簡單:
$headerValue = $_SERVER['HTTP_X_REQUESTED_WITH'];
或者當你需要一次獲得一個時:
/**
* @param $pHeaderKey
* @return mixed
*/
function get_header( $pHeaderKey )
{
// Expanded for clarity.
$headerKey = str_replace('-', '_', $pHeaderKey);
$headerKey = strtoupper($headerKey);
$headerValue = NULL;
// Uncomment the if when you do not want to throw an undefined index error.
// I leave it out because I like my app to tell me when it can't find something I expect.
//if ( array_key_exists($headerKey, $_SERVER) ) {
$headerValue = $_SERVER[ $headerKey ];
//}
return $headerValue;
}
// X-Requested-With mainly used to identify Ajax requests. Most JavaScript frameworks
// send this header with value of XMLHttpRequest, so this will not always be present.
$header_x_requested_with = get_header( 'X-Requested-With' );
其他標題也在超全局數組$ _SERVER中,您可以在這里閱讀有關如何獲取它們的信息:[http://php.net/manual/en/reserved.variables.server.php]
b01 answered 2019-02-28T12:16:12Z
2 votes
我正在使用CodeIgniter并使用下面的代碼來獲取它。 可能對將來有用。
$this->input->get_request_header('X-Requested-With');
Rajesh answered 2019-02-28T12:16:40Z
1 votes
這就是我在做的方式。 如果未傳遞$ header_name,則需要獲取所有標頭:
function getHeaders($header_name=null)
{
$keys=array_keys($_SERVER);
if(is_null($header_name)) {
$headers=preg_grep("/^HTTP_(.*)/si", $keys);
} else {
$header_name_safe=str_replace("-", "_", strtoupper(preg_quote($header_name)));
$headers=preg_grep("/^HTTP_${header_name_safe}$/si", $keys);
}
foreach($headers as $header) {
if(is_null($header_name)){
$headervals[substr($header, 5)]=$_SERVER[$header];
} else {
return $_SERVER[$header];
}
}
return $headervals;
}
print_r(getHeaders());
echo "\n\n".getHeaders("Accept-Language");
?>
對我來說,它看起來比其他答案中給出的大多數例子簡單得多。 這也獲取方法(GET / POST / etc。)和獲取所有標頭時請求的URI,如果您嘗試在日志記錄中使用它,這可能很有用。
這是輸出:
Array ( [HOST] => 127.0.0.1 [USER_AGENT] => Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:28.0) Gecko/20100101 Firefox/28.0 [ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 [ACCEPT_LANGUAGE] => en-US,en;q=0.5 [ACCEPT_ENCODING] => gzip, deflate [COOKIE] => PHPSESSID=MySessionCookieHere [CONNECTION] => keep-alive )
en-US,en;q=0.5
Jonnycake answered 2019-02-28T12:17:26Z
0 votes
這是一個簡單的方法。
// echo get_header('X-Requested-With');
function get_header($field) {
$headers = headers_list();
foreach ($headers as $header) {
list($key, $value) = preg_split('/:\s*/', $header);
if ($key == $field)
return $value;
}
}
kehers answered 2019-02-28T12:17:57Z
0 votes
這個小的PHP代碼段對您有所幫助:
foreach($_SERVER as $key => $value){
echo '$_SERVER["'.$key.'"] = '.$value."
";
}
?>
Technolust answered 2019-02-28T12:18:25Z
0 votes
function getCustomHeaders()
{
$headers = array();
foreach($_SERVER as $key => $value)
{
if(preg_match("/^HTTP_X_/", $key))
$headers[$key] = $value;
}
return $headers;
}
我使用此函數來獲取自定義標頭,如果標頭從“HTTP_X_”開始我們推入數組:)
ZiTAL answered 2019-02-28T12:18:54Z
0 votes
如果只需要一個密鑰來檢索,例如需要"Host"地址,那么我們就可以使用了
apache_request_headers()['Host']
這樣我們就可以避免循環并將其內聯到echo輸出中
Zigma Empire answered 2019-02-28T12:19:31Z
-1 votes
如果您有Apache服務器,這項工作
PHP代碼:
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value
\n";
}
結果:
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0
Host: www.example.com
Connection: Keep-Alive
Emanuel Nogueiras answered 2019-02-28T12:20:13Z
總結
以上是生活随笔為你收集整理的accept标头 php,如何在PHP中读取任何请求标头的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: echart x轴标签偏移_1文搞懂真正
- 下一篇: 电脑计算机d盘有用吗,d盘不见了,教您电