最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Wordpress shortcode using WPB_GET_IP

programmeradmin1浏览0评论

I am using a shortcode in Wordpress to capture the visitor's IP address. This is working except for one thing. For one visitor, two IP addresses separated by a comma was captured with the shortcode below. The 2nd IP doesn't seem to exist with nslookup. I have added my fix to grab the first IP, but I would love to know how this is possible.

obfuscated "24.158.999.999, 104.129.999.999"

 function get_visitor_ip() {
    if (!empty( $_SERVER['HTTP_CLIENT_IP'])) {
    //check ip from internet
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    //to check ip is pass from proxy
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
    $ip = $_SERVER['REMOTE_ADDR'];
    }
    return apply_filters('wpb_get_ip', $ip);
 }
 add_shortcode('visitor_ip', 'get_visitor_ip');

I am using a shortcode in Wordpress to capture the visitor's IP address. This is working except for one thing. For one visitor, two IP addresses separated by a comma was captured with the shortcode below. The 2nd IP doesn't seem to exist with nslookup. I have added my fix to grab the first IP, but I would love to know how this is possible.

obfuscated "24.158.999.999, 104.129.999.999"

 function get_visitor_ip() {
    if (!empty( $_SERVER['HTTP_CLIENT_IP'])) {
    //check ip from internet
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    //to check ip is pass from proxy
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
    $ip = $_SERVER['REMOTE_ADDR'];
    }
    return apply_filters('wpb_get_ip', $ip);
 }
 add_shortcode('visitor_ip', 'get_visitor_ip');
Share Improve this question asked Feb 1, 2021 at 1:07 Gregory BolognaGregory Bologna 1214 bronze badges 1
  • 1 I'd guess you had two X-Forwarded-For headers from two layers of proxies, but I've never seen that myself. – Rup Commented Feb 1, 2021 at 10:56
Add a comment  | 

1 Answer 1

Reset to default 1

See the accepted answer at ServerFault:

[...] if a request is chained through more than one proxy server, then each proxy should add the IP of the preceding one to the existing X-Forwarded-For header so that the entire chain is preserved.

X-Forwarded-For: <client>, <proxy1>, <proxy2>, ...

发布评论

评论列表(0)

  1. 暂无评论