2

I use Vladimir Popov(?)'s very nice Webforms extension to enable user submitted custom forms. I want to capture all the http header information from the user who is submitting the form. Vladimir has kindly supplied me with a php call that works in a hidden field in the form, for him.

<?php foreach(getallheaders() as $key=>$value){echo $key.": ".$value."\n";}; ?>

this works for him on his environment for either logged in or not logged in users and produces the attached output. Unfortunately, it does not work for me on my Magento 1.9.2.2 environment. I don't get any information at all. Vladimir can only suggest that there is something about my server configuration. It looks as if this is either a php issue or a data model issue, and I know very little about either. Can someone who is an expert help me figure out what's going on?enter image description here

2

The method getallheaders is simply an alias for apache_request_headers. If you are using a fast-cgi webserver and older version of PHP then this information might not be populated. Most of the headers you want are in the $_SERVER array. So your best bet is probably to create your own version of the command:

// apache_request_headers replicement for nginx
if (!function_exists('apache_request_headers')) { 
    function apache_request_headers() { 
        foreach($_SERVER as $key=>$value) { 
            if (substr($key,0,5)=="HTTP_") { 
                $key=str_replace(" ","-",ucwords(strtolower(str_replace("_"," ",substr($key,5))))); 
                $out[$key]=$value; 
            }else{
                $out[$key]=$value; 
    }
        } 
        return $out; 
    } 
}
| improve this answer | |
  • thank you! where is the $_SERVER array and what is a safe method of making the change? – Fred Zimmerman Feb 9 '16 at 1:53
  • $_SERVER is a PHP super global (it's available anywhere). There is no nice way to add global functions to Magento, so without knowing where you were calling getallheaders it's a bit hard to give an accurate answer. The most 'correct' solution would be to create a helper method that returns the info, but this will require some Magento knowledge. The simplest solution will be to be to define the function wherever you're calling getallheaders. – Peter O'Callaghan Feb 9 '16 at 11:28
1

I'm assuming you're using Magento (based on the fact this is magento.stackoexchange.com). It is bad form to use webserver-specific functions in application level code. Magento has built in utilities to get header values, inherited from Zend_Controller_Request_Http.

This will get you the header by name:

Mage::app()->getRequest()->getHeader('headername');

If you want all http related servers, you can recycle this snippet from Zend_Controller_Request_Http, or extend the class. Zend_Controller_Request_Http does not have a function to blindly get all headers AFAIK.

...
// Try to get it from the $_SERVER array first
$temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
if (isset($_SERVER[$temp])) {
    return $_SERVER[$temp];
}
...
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.