How do I read any request header in PHP

2019-01-01 03:04发布

How should I read any header in PHP?

For example the custom header: X-Requested-With.

14条回答
浪荡孟婆
2楼-- · 2019-01-01 03:56

This work if you have an Apache server

PHP Code:

$headers = apache_request_headers();

foreach ($headers as $header => $value) {
    echo "$header: $value <br />\n";
}

Result:

Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0
Host: www.example.com
Connection: Keep-Alive
查看更多
荒废的爱情
3楼-- · 2019-01-01 04:00
$_SERVER['HTTP_X_REQUESTED_WITH']

RFC3875, 4.1.18:

Meta-variables with names beginning with HTTP_ contain values read from the client request header fields, if the protocol used is HTTP. The HTTP header field name is converted to upper case, has all occurrences of - replaced with _ and has HTTP_ prepended to give the meta-variable name.

查看更多
登录 后发表回答