Make WordPress Core

Changes between Initial Version and Version 1 of Ticket #54482, comment 1


Ignore:
Timestamp:
11/21/2021 10:40:05 AM (3 years ago)
Author:
lxtx
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #54482, comment 1

    initial v1  
     1Got it, [https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/#comment-4830], this example has a little problem.
     2
     3After I change it as below, the issue is fixed.
     4
     5{{{#!php
     6add_filter( 'pre_comment_user_ip', 'auto_reverse_proxy_pre_comment_user_ip');
     7function auto_reverse_proxy_pre_comment_user_ip($comment_author_IP) {
     8    if( is_admin() ){
     9        return $comment_author_IP;
     10    }
     11   
     12    $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
     13 
     14    if (!empty($_SERVER['X_FORWARDED_FOR'])) {
     15        $X_FORWARDED_FOR = explode(',', $_SERVER['X_FORWARDED_FOR']);
     16        if (!empty($X_FORWARDED_FOR)) {
     17            $REMOTE_ADDR = trim($X_FORWARDED_FOR[0]);
     18        }
     19    }
     20 
     21    /*
     22    * Some PHP environments will use the $_SERVER['HTTP_X_FORWARDED_FOR']
     23    * variable to capture visitor address information.
     24    */
     25    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
     26        $HTTP_X_FORWARDED_FOR= explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
     27        if (!empty($HTTP_X_FORWARDED_FOR)) {
     28            $REMOTE_ADDR = trim($HTTP_X_FORWARDED_FOR[0]);
     29        }
     30    }
     31 
     32    return preg_replace('/[^0-9a-f:\., ]/si', '', $REMOTE_ADDR);
     33}
     34}}}