[wp-trac] Re: [WordPress Trac] #9235: Extract real IP behind a load balancer

WordPress Trac wp-trac at lists.automattic.com
Thu Jun 11 22:23:50 GMT 2009


#9235: Extract real IP behind a load balancer
-------------------------------+--------------------------------------------
 Reporter:  Denis-de-Bernardy  |       Owner:  Denis-de-Bernardy            
     Type:  enhancement        |      Status:  accepted                     
 Priority:  normal             |   Milestone:  2.9                          
Component:  Optimization       |     Version:  2.7                          
 Severity:  normal             |    Keywords:  has-patch tested commit early
-------------------------------+--------------------------------------------

Comment(by robertaccettura):

 Just kinda freehanding... but maybe this can be simplified to handle
 things like TP_X_FORWARDED_FOR a little easier...

 I just freehanded this for a proposed example... not saying this is
 perfect, or even sane... just putting ideas forward.

 {{{
 /**
  * If you're behind a load-balancer or CDN, you may want to set
  * WP_REMOTE_ADDR_HEADER to the correct header.  If comma delimited,
  * it will fetch the first IP address.
  */
 define('WP_REMOTE_ADDR_HEADER', 'X-Real-IP');

 /**
  * There are quite a few more defines for advanced users. See the wp-
 settings.php
  * file for details.
  */

 }}}




 {{{
 /**
  * Fix remote address behind a load balancer
  *
  * If what follows doesn't work with your setup, configure the define in
 your wp-config.php file
  *
  * @since 2.9
  */
 if( defined('WP_REMOTE_ADDR_HEADER') ) {
         if( isset($_SERVER[WP_REMOTE_ADDR_HEADER]) ){
                 if( strpos($_SERVER[WP_REMOTE_ADDR_HEADER],',') !==
 false){
                         $remote_ip = explode(',',
 $_SERVER[WP_REMOTE_ADDR_HEADER]);
                         $remote_ip = $remote_ip[0];
                 } else {
                         $remote_ip = $_SERVER['WP_REMOTE_ADDR_HEADER'];
                 }
                 if( preg_match('/(\d+).(\d+).(\d+).(\d+)/',$remote_ip) ){
                         $_SERVER['REMOTE_ADDR'] = $remote_ip;
                 }
                 unset($remote_ip)
         }
 }
 }}}

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/9235#comment:20>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list