Call to undefined function apache_request_headers()

前端 未结 6 945
广开言路
广开言路 2020-11-27 04:58

I\'ve just switched my scripts to a different server. On the previous server this worked flawlessly, and now that I\'ve switched them to a different server, I can\'t underst

相关标签:
6条回答
  • 2020-11-27 05:31

    As suggested in the other answer here, I have used the function from the comments in the PHP documentation, but found that it's suboptimal, hard to read/maintain, and not complete compared to the (non-conforming) casing of some headers.

    So because I needed to really be able to rely on it, I recoded it to be more obvious and handle more edge-cases better as well – the original code even states "do some nasty string manipulations to restore the original letter case" and "this should work in most cases", which doesn't sound nice for something you should be able to depend on.

    It's not perfect, but I find that it's more reliable. One thing it lacks is to work on the actual or original headers, as any modifications to $_SERVER will be reflected in the output. This can be mitigated by making the result static and running the function as the first thing on every request.

    <?php
    // Drop-in replacement for apache_request_headers() when it's not available
    if(!function_exists('apache_request_headers')) {
        function apache_request_headers() {
    
            // Based on: http://www.iana.org/assignments/message-headers/message-headers.xml#perm-headers
            $arrCasedHeaders = array(
                // HTTP
                'Dasl'             => 'DASL',
                'Dav'              => 'DAV',
                'Etag'             => 'ETag',
                'Mime-Version'     => 'MIME-Version',
                'Slug'             => 'SLUG',
                'Te'               => 'TE',
                'Www-Authenticate' => 'WWW-Authenticate',
                // MIME
                'Content-Md5'      => 'Content-MD5',
                'Content-Id'       => 'Content-ID',
                'Content-Features' => 'Content-features',
            );
            $arrHttpHeaders = array();
    
            foreach($_SERVER as $strKey => $mixValue) {
                if('HTTP_' !== substr($strKey, 0, 5)) {
                    continue;
                }
    
                $strHeaderKey = strtolower(substr($strKey, 5));
    
                if(0 < substr_count($strHeaderKey, '_')) {
                    $arrHeaderKey = explode('_', $strHeaderKey);
                    $arrHeaderKey = array_map('ucfirst', $arrHeaderKey);
                    $strHeaderKey = implode('-', $arrHeaderKey);
                }
                else {
                    $strHeaderKey = ucfirst($strHeaderKey);
                }
    
                if(array_key_exists($strHeaderKey, $arrCasedHeaders)) {
                    $strHeaderKey = $arrCasedHeaders[$strHeaderKey];
                }
    
                $arrHttpHeaders[$strHeaderKey] = $mixValue;
            }
    
            return $arrHttpHeaders;
    
        }
    }
    
    0 讨论(0)
  • 2020-11-27 05:34

    Pleae use follownig methord to get headers using PHP in Nginx

    function getHeaders()
    {
    $headers = array();
    foreach ($_SERVER as $k => $v)
    {
    if (substr($k, 0, 5) == "HTTP_")
    {
    $k = str_replace('_', ' ', substr($k, 5));
    $k = str_replace(' ', '-', ucwords(strtolower($k)));
    $headers[$k] = $v;
    }
    }
    return $headers;
    }
    

    Source from this page

    0 讨论(0)
  • 2020-11-27 05:35

    You can use the following replacement function:

    <?php
    if( !function_exists('apache_request_headers') ) {
    ///
    function apache_request_headers() {
      $arh = array();
      $rx_http = '/\AHTTP_/';
      foreach($_SERVER as $key => $val) {
        if( preg_match($rx_http, $key) ) {
          $arh_key = preg_replace($rx_http, '', $key);
          $rx_matches = array();
          // do some nasty string manipulations to restore the original letter case
          // this should work in most cases
          $rx_matches = explode('_', $arh_key);
          if( count($rx_matches) > 0 and strlen($arh_key) > 2 ) {
            foreach($rx_matches as $ak_key => $ak_val) $rx_matches[$ak_key] = ucfirst($ak_val);
            $arh_key = implode('-', $rx_matches);
          }
          $arh[$arh_key] = $val;
        }
      }
      return( $arh );
    }
    ///
    }
    ///
    ?>
    

    Source: PHP Manual

    0 讨论(0)
  • 2020-11-27 05:39

    From the docs, before the release of PHP 5.4.0:

    This function is only supported when PHP is installed as an Apache module.

    PHP 5.4.0 and later support this function unconditionally.

    Said docs also include replacement functions that mimic the functionality of apache_request_headers by stepping through $_SERVER.

    0 讨论(0)
  • 2020-11-27 05:47

    if php is installed as an Apache module:

    apache_request_headers()["Authorization"];
    

    else, go to .htaccess file and add:

    SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
    

    You can then access request headers using any of these:

    $_SERVER["HTTP_AUTHORIZATION"]; // using super global
    

    OR

    $app->request->headers("Authorization"); // if using PHP Slim
    
    0 讨论(0)
  • 2020-11-27 05:47

    same thing happened to me when using "apache_request_headers()" so i used this code - works perfectly for me to output all the headers:

    <?php
    
        $headers = array();
    
        foreach($_SERVER as $key => $value) {
            if(strpos($key, 'HTTP_') === 0) {
                $headers = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
                echo $headers." : ". $i[$headers] = $value . "<br>";
            }
        }
    
    ?>
    

    output example:

    Accept : text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Encoding : gzip, deflate
    Accept-Language : en-US,en;q=0.5
    Cache-Control : max-age=0
    Connection : keep-alive
    Host : example.com
    Referer : https://example.com/
    User-Agent : Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
    
    0 讨论(0)
提交回复
热议问题