2012-11-05 18 views
30

È possibile ottenere le intestazioni http della richiesta corrente con PHP? Sono non utilizzando Apache come server Web, ma utilizzando nginx.Ottieni le intestazioni http dalla richiesta corrente in PHP

Ho provato a utilizzare getallheaders() ma sto ottenendo Call to undefined function getallheaders().

+0

come si può vedere nella mia risposta è comunque possibile utilizzare getallheaders() – gabrielem

+0

andare a votare qui: https://bugs.php.net/bug.php?id = 62596 – Bell

risposta

3

È possibile aggiornare il server a PHP 5.4 dando quindi accesso a getallheaders() tramite fastcgi o semplicemente analizzando ciò che è necessario da $ _SERVER con un ciclo foreach e un po 'di espressione regolare.

+1

'nginx' funziona sempre su FastCGI? È per questo che 'getallheaders()' non funziona con PHP 5.3? –

+1

@BenHarold Vedi il registro delle modifiche di [getallheaders] (http://www.php.net/manual/en/function.getallheaders.php): _5.4: questa funzione è diventata disponibile in FastCGI. In precedenza, era supportato solo quando PHP era installato come modulo Apache. –

+0

@FredWuerges Ho letto il log delle modifiche. Ecco perché ho fatto le domande. Per dirla un po 'meglio: nginx usa sempre FastCGI, ed è per questo che 'getallheaders()' non funziona quando si usa PHP 5.3 o precedente con nginx? Questo significa che entrambi 'getallheaders()' e 'apache_request_headers()' funzionano quando si usano PHP 5.4 e nginx? –

43

Tratto da documentazione qualcuno ha scritto un comment ...

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; 
    } 
} 
+1

Grazie funziona. Ma potresti spiegare qual è lo scopo di 'ucwords' e' strtolower' in quella funzione? È necessario ? – Cone

+1

Un bug in questa funzione è che le intestazioni maiuscole come "DNT" (Do Not Track) diventeranno "Dnt" - che NON è il caso di getallheader nativi() – Bell

20

Migliorato @Layke la sua funzione, il che rende un po 'più sicuro usarlo:

if (!function_exists('getallheaders')) { 
    function getallheaders() 
    { 
     if (!is_array($_SERVER)) { 
      return array(); 
     } 

     $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; 
    } 
} 

(voluto solo potessi aggiungi questo come commento alla sua risposta, ma continua a costruire su quella reputazione thingy - una delle mie prime risposte)

1

combinato ottenere allheaders() + apache_request_headers() per 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; 
} 
Problemi correlati