Get the http headers from current request in PHP

后端 未结 6 1859
梦谈多话
梦谈多话 2020-12-08 04:10

Is it possible to get the http headers of the current request with PHP? I am not using Apache as the web-server, but using nginx.

I tried using

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 04:40

    Combined getallheaders() + apache_request_headers() for nginx

        function get_nginx_headers($function_name='getallheaders'){
    
            $all_headers=array();
    
            if(function_exists($function_name)){ 
    
                $all_headers=$function_name();
            }
            else{
    
                foreach($_SERVER as $name => $value){
    
                    if(substr($name,0,5)=='HTTP_'){
    
                        $name=substr($name,5);
                        $name=str_replace('_',' ',$name);
                        $name=strtolower($name);
                        $name=ucwords($name);
                        $name=str_replace(' ', '-', $name);
    
                        $all_headers[$name] = $value; 
                    }
                    elseif($function_name=='apache_request_headers'){
    
                        $all_headers[$name] = $value; 
                    }
                }
            }
    
    
            return $all_headers;
    }
    

提交回复
热议问题