Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Strange PHP Issue
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Strange PHP Issue

xaitmixaitmi Member

I have CentMinMod running on a server.

<?
function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share 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 $ip;
}

echo getRealIpAddr();
?>

Basically it returns this.

MyIPaddress, MyIPAddress

so like

99.XX.XX.XXX,99.XX.XX.XXX

Anyone know why it does that? I only need my IP to show once, because it's causing problems with an application I am coding.

Comments

  • Is this the full code or just part? Reason I ask is because may the function is getting called twice from somewhere else.

  • FranciscoFrancisco Top Host, Host Rep, Veteran

    HTTP_X_FORWARDED_FOR is getting populated with it.

    Do print_r($_SERVER); and you'll see the same thing from it.

    Francisco

  • @CFarence said:
    Is this the full code or just part? Reason I ask is because may the function is getting called twice from somewhere else.

    thats the full php code in that file.

  • draziloxdrazilox Member
    edited April 2016

    return explode(',', $ip)[0];

    EDIT: This might not be so good answer. It just takes the first IP.

  • xaitmixaitmi Member
    edited April 2016

    @Francisco said:
    HTTP_X_FORWARDED_FOR is getting populated with it.

    Do print_r($_SERVER); and you'll see the same thing from it.

    Francisco

    here is the output of your command. what do you recommend?

    http://pastebin.com/crW8nduf

  • FranciscoFrancisco Top Host, Host Rep, Veteran

    @xaitmi said:

    What @drazilox said isn't bad, but you'll likely want to skip the first entry since it's sometimes 127.0.0.1.

    Francisco

  • All right. Thanks for the quick help everyone.

    Added in
    return explode(',', $ip)[1];

  • draziloxdrazilox Member
    edited April 2016

    I'm not sure if HTTP_X_FORWARDED_FOR always returns multiple IPs. When it doensn't, then that fails. Maybe it's better to grab the last one.

    end(explode(',', $ip));

    EDIT: Check the comments on this http://stackoverflow.com/a/24275009

  • What proxy do you have in front of it?

  • xaitmixaitmi Member
    edited April 2016

    @CFarence said:
    What proxy do you have in front of it?

    nginx reverse proxy on another server with this config

    server {
          listen        443;
          server_name    domain.com;
          access_log   /home/logs/cv.access.log;
          error_log /home/logs/cv.error.log;
    
          location / {
                proxy_pass https://webserveriphere/;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-SSL on;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_max_temp_file_size 0;
                client_max_body_size 10m;
                client_body_buffer_size 128k;
                proxy_connect_timeout 90;
                proxy_send_timeout 90;
                proxy_read_timeout 90;
                proxy_buffer_size 4k;
                proxy_buffers 4 32k;
                proxy_busy_buffers_size 64k;
                proxy_temp_file_write_size 64k;
    
    #CloudFlare
    set_real_ip_from 199.27.128.0/21;
    set_real_ip_from 173.245.48.0/20;
    set_real_ip_from 103.21.244.0/22;
    set_real_ip_from 103.22.200.0/22;
    set_real_ip_from 103.31.4.0/22;
    set_real_ip_from 141.101.64.0/18;
    set_real_ip_from 108.162.192.0/18;
    set_real_ip_from 190.93.240.0/20;
    set_real_ip_from 188.114.96.0/20;
    set_real_ip_from 197.234.240.0/22;
    set_real_ip_from 198.41.128.0/17;
    set_real_ip_from 162.158.0.0/15;
    set_real_ip_from 104.16.0.0/12;
    set_real_ip_from 172.64.0.0/13;
    real_ip_header CF-Connecting-IP;
          }
    }
    
    
    

    Basically

    Cloudflare --> OVH VPS Reverse Proxy --> VPS with website on it

  • http://phptester.net/ Test your PHP code online without the need of a web server. (PHP 5.2, 5.3 and 5.4).

Sign In or Register to comment.