function detect real ip (updated)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    function detect real ip (updated)

    PHP Code:
    function gum_real_ip()
    {
         
    $keyname_ip_arr = array('HTTP_X_FORWARDED_FOR''HTTP_REMOTE_ADDR_REAL''HTTP_CLIENT_IP''HTTP_X_REAL_IP''REMOTE_ADDR');
        foreach (
    $keyname_ip_arr as $keyname_ip) {
      if (!empty(
    $_SERVER[$keyname_ip])) {
       
    $ip $_SERVER[$keyname_ip];
       if (
    strstr($ip',')) {
        
    $ips explode(','$ip);
        foreach (
    $ips as $ip)
        {
          if(
    filter_var(trim($ip), FILTER_VALIDATE_IPFILTER_FLAG_NO_PRIV_RANGE)) break(2);
        }
       }
       elseif(
    filter_var($ipFILTER_VALIDATE_IPFILTER_FLAG_NO_PRIV_RANGE)) break;
      }
        }
        return 
    $ip;
        list(
    $ip1,$ip2,$ip3,$ip4) = explode('.',$ip);
        return 
    $ip1.'.'.$ip2.'.'.$ip3.'.0'//REAL IP

    Advertise your mobile site for FREE with AdTwirl


    #2
    Originally posted by GumSlone View Post
    PHP Code:
    function gum_real_ip()
    {

        
    $keyname_ip_arr = array('HTTP_X_FORWARDED_FOR''HTTP_REMOTE_ADDR_REAL''HTTP_CLIENT_IP''HTTP_X_REAL_IP''REMOTE_ADDR');
        foreach (
    $keyname_ip_arr as $keyname_ip) {
            if (!empty(
    $_SERVER[$keyname_ip])) {
                
    $ip urlencode($_SERVER[$keyname_ip]);
                break;
            }
        }
        if (
    strstr($ip',')) {
            
    $ips explode(','$ip);
            
    $ip $ips[0];
        }
        if(empty(
    $ip)) $ip $_SERVER["REMOTE_ADDR"];
        
        list(
    $ip1,$ip2,$ip3,$ip4) = explode('.',$ip);
        return 
    $ip1.'.'.$ip2.'.'.$ip3.'.0'//REAL IP

    is this will work on ngix wid mini opera nd proxies ??
    and last subnet 0 ?? isnt it wil return 0 in the last of all ip's ??

    Comment


      #3
      Originally posted by Anshul View Post
      is this will work on ngix wid mini opera nd proxies ??
      and last subnet 0 ?? isnt it wil return 0 in the last of all ip's ??
      yes it will return the last 0 for all ips,
      and it should work with nginx and opera and other proxies, this it the latest function which i am using on my sites,
      well,
      if you dont want to have the last number in ip =0;
      you can simply replace :
      PHP Code:
          list($ip1,$ip2,$ip3,$ip4) = explode('.',$ip); 
          return 
      $ip1.'.'.$ip2.'.'.$ip3.'.0'//REAL IP 
      by
      PHP Code:
      return $ip
      also if you dont use apache's mod_geo_ip for ip to coutry detection its better to set last ip value to zero and cache the result of geo ip detection for this ip eg. with memcached, this will reduce the server load.
      Advertise your mobile site for FREE with AdTwirl

      Comment


        #4
        gumslone.. but they put in the core, or in the user profile?

        Comment


          #5
          Originally posted by Leviathan73 View Post
          gumslone.. but they put in the core, or in the user profile?
          put it wherever you want to detect the ip
          Advertise your mobile site for FREE with AdTwirl

          Comment


            #6
            thanks gumslone

            Comment


              #7
              Originally posted by GumSlone View Post
              yes it will return the last 0 for all ips,
              and it should work with nginx and opera and other proxies, this it the latest function which i am using on my sites,
              well,
              if you dont want to have the last number in ip =0;
              you can simply replace :
              PHP Code:
                  list($ip1,$ip2,$ip3,$ip4) = explode('.',$ip); 
                  return 
              $ip1.'.'.$ip2.'.'.$ip3.'.0'//REAL IP 
              by
              PHP Code:
              return $ip
              also if you dont use apache's mod_geo_ip for ip to coutry detection its better to set last ip value to zero and cache the result of geo ip detection for this ip eg. with memcached, this will reduce the server load.
              ok cool i dont use seperate queries for updatin ip anyway its wid last active time anyway i wil giv a try to this

              Comment


                #8
                i have updated the function
                Advertise your mobile site for FREE with AdTwirl

                Comment


                  #9
                  is relying on HTTP_X_FORWARDED_FOR as your first choice to get real ip a good idea ?
                  As it is probably the most common way of spoofing ip number

                  Comment


                    #10
                    Originally posted by something else View Post
                    is relying on HTTP_X_FORWARDED_FOR as your first choice to get real ip a good idea ?
                    As it is probably the most common way of spoofing ip number
                    well, the HTTP_X_FORWARDED_FOR will work wth all opera mini users.
                    Advertise your mobile site for FREE with AdTwirl

                    Comment


                      #11
                      Yeah i was going to say you could use something like:
                      PHP Code:
                      if(!isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']))
                               
                      //re-arange array 
                      however this will also miss poor proxys also
                      so maybe also an else statement something like:
                      PHP Code:
                      else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])&&$_SERVER['HTTP_X_FORWARDED_FOR']!=$_SERVER['REMOTE_ADDR'])
                      $ip $_SERVER['HTTP_X_FORWARDED_FOR']; 
                      also finally to prevent hacking:
                      PHP Code:
                      return (int)$ip1.'.'.(int)$ip2.'.'.(int)$ip3.'.0'//REAL IP 

                      Comment


                        #12
                        Originally posted by something else View Post
                        Yeah i was going to say you could use something like:
                        PHP Code:
                        if(!isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']))
                                 
                        //re-arange array 
                        however this will also miss poor proxys also
                        so maybe also an else statement something like:
                        PHP Code:
                        else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])&&$_SERVER['HTTP_X_FORWARDED_FOR']!=$_SERVER['REMOTE_ADDR'])
                        $ip $_SERVER['HTTP_X_FORWARDED_FOR']; 
                        also finally to prevent hacking:
                        PHP Code:
                        return (int)$ip1.'.'.(int)$ip2.'.'.(int)$ip3.'.0'//REAL IP 
                        if someone wants to change/hide ip he can do it easily and there is no way to detect a real ip in such cases,
                        this function is made to detect ip of other 99% normal site users who use opera or use other different proxys to save their traffic costs and not trying to hide their location/ip, you could also add a function which checks if the detected ip is valid and if not then skip back to remote_addr.
                        Advertise your mobile site for FREE with AdTwirl

                        Comment


                          #13
                          I discard everything invalid, even with commas.

                          PHP Code:
                          function user_ip() {
                          if (!empty(
                          $_SERVER['HTTP_CLIENT_IP'])) {
                          $ip $_SERVER['HTTP_CLIENT_IP'];
                          }
                          elseif (!empty(
                          $_SERVER['HTTP_X_FORWARDED_FOR'])) {
                          $ip $_SERVER['HTTP_X_FORWARDED_FOR'];
                          }
                          elseif (!empty(
                          $_SERVER['HTTP_X_FORWARDED'])) {
                          $ip $_SERVER['HTTP_X_FORWARDED'];
                          }
                          elseif (!empty(
                          $_SERVER['HTTP_FORWARDED_FOR'])) {
                          $ip $_SERVER['HTTP_FORWARDED_FOR'];
                          }
                          elseif (!empty(
                          $_SERVER['HTTP_FORWARDED'])) {
                          $ip $_SERVER['HTTP_FORWARDED'];
                          } else {
                          $ip $_SERVER['REMOTE_ADDR'];
                          }
                          if (isset(
                          $ip) && !preg_match('/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/'$ip)) {
                          $ip 'ProxyIP';
                          }
                          return 
                          $ip;

                          <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

                          Comment


                            #14
                            Originally posted by arnage View Post
                            I discard everything invalid, even with commas.

                            PHP Code:
                            function user_ip() {
                            if (!empty(
                            $_SERVER['HTTP_CLIENT_IP'])) {
                            $ip $_SERVER['HTTP_CLIENT_IP'];
                            }
                            elseif (!empty(
                            $_SERVER['HTTP_X_FORWARDED_FOR'])) {
                            $ip $_SERVER['HTTP_X_FORWARDED_FOR'];
                            }
                            elseif (!empty(
                            $_SERVER['HTTP_X_FORWARDED'])) {
                            $ip $_SERVER['HTTP_X_FORWARDED'];
                            }
                            elseif (!empty(
                            $_SERVER['HTTP_FORWARDED_FOR'])) {
                            $ip $_SERVER['HTTP_FORWARDED_FOR'];
                            }
                            elseif (!empty(
                            $_SERVER['HTTP_FORWARDED'])) {
                            $ip $_SERVER['HTTP_FORWARDED'];
                            } else {
                            $ip $_SERVER['REMOTE_ADDR'];
                            }
                            if (isset(
                            $ip) && !preg_match('/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/'$ip)) {
                            $ip 'ProxyIP';
                            }
                            return 
                            $ip;

                            the ip range with comma contains the real ip of opera mini user, i use my function for ip to country detection, because its more correct than the default mod geoip which detects country by remote_addr.
                            Advertise your mobile site for FREE with AdTwirl

                            Comment


                              #15
                              hello guys,


                              why we using HTTP_X_FORWARDED_FOR', 'HTTP_REMOTE_ADDR_REAL', 'HTTP_CLIENT_IP', 'HTTP_X_REAL_IP', 'REMOTE_ADDR, to detect ip ???


                              all these are similar to REMOTE_ADDR ??

                              Comment

                              Working...
                              X