Php methods for obtaining http request header information
-
- Foreach (getallheaders () as $ name => $ value ){
- Echo "$ name: $ value \ n ";
- }
- ?>
-
However, this function can only be used in an apache environment. it is not supported by iis or nginx and can be implemented through custom functions.
- If (! Function_exists ('getallheaders '))
- {
- Function getallheaders ()
- {
- Foreach ($ _ SERVER as $ name => $ value)
- {
- If (substr ($ name, 0, 5) = 'http _')
- {
- $ Headers [str_replace ('', '-', ucwords (strtolower (str_replace ('_','', substr ($ name, 5)] = $ value;
- }
- }
- Return $ headers;
- }
- }
- ?>
Check the output result.
- Print_r (getallheaders ());
- ?>
Result: Array ([Accept] => */* [Accept-Language] => zh-cn [Accept-Encoding] => gzip, deflate [User-Agent] => Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0 ;. net clr 2.0.50727) [Host] => localhost [Connection] => Keep-Alive) |