cancel
Showing results for 
Search instead for 
Did you mean: 

How do I specify alternative headers for Magento\Framework\HTTP\PhpEnvironment\RemoteAddress.php

SOLVED

How do I specify alternative headers for Magento\Framework\HTTP\PhpEnvironment\RemoteAddress.php

I'm building a CloudFlare plugin for Magento2 and am currently working on restoring the visitor's real IP address.  CloudFlare sends the visitor's IP address in a HTTP_CF_CONNECTING_IP header and I see that https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/HTTP/PhpEnvironment/... will set the IP address to the alternative header if it exists but I can't figure out how to specify extra alternative headers.

 

I can't check $_SERVER in a before or after method because the magento extension validation disallows the use of super globals.

 

How can I specify HTTP_CF_CONNECTING_IP as an alternative header for RemoteAddress?

 

Thanks,

John

1 REPLY

Re: How do I specify alternative headers for Magento\Framework\HTTP\PhpEnvironment\RemoteAddress.php

I'm an idiot.  Solution is:

 

etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <!-- Restore original visitor IP from CloudFlare header -->
    <type name="Magento\Framework\HTTP\PhpEnvironment\RemoteAddress">
        <arguments>
            <argument name="alternativeHeaders" xsi:type="array">
                <item name="http_cf_connecting_ip" xsi:type="string">HTTP_CF_CONNECTING_IP</item>
            </argument>
        </arguments>
    </type>
</config>