Subversion Repositories php_utils

Rev

Rev 5 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * IP functions
  5.  * Copyright 2015-2022 Daniel Marschall, ViaThinkSoft
  6.  * Version 2021-01-07
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  */
  20.  
  21. // Attention in re $allow_proxy: It is not secure to use these, since they are not validated: http://www.thespanner.co.uk/2007/12/02/faking-the-unexpected/
  22. function get_real_ip($allow_proxy=false) {
  23.         /* Eindeutige IP Adresse erhalten, auch bei Proxies und (neu:) von SSH connections im CLI modus */
  24.         // http://lists.phpbar.de/pipermail/php/Week-of-Mon-20040322/007749.html
  25.         // Modificated by VTS
  26.         // Version: 2021-01-07
  27.  
  28.         // TODO: ipv6
  29.  
  30.         if (isset($_SERVER['SSH_CLIENT']))     { $ary = explode(' ', $_SERVER['SSH_CLIENT']);     return $ary[0]; }
  31.         if (isset($_SERVER['SSH_CONNECTION'])) { $ary = explode(' ', $_SERVER['SSH_CONNECTION']); return $ary[0]; }
  32.  
  33.         $client_ip       = ($allow_proxy && isset($_SERVER['HTTP_CLIENT_IP']))       ? $_SERVER['HTTP_CLIENT_IP']       : '';
  34.         $x_forwarded_for = ($allow_proxy && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '';
  35.         $remote_addr     = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '';
  36.  
  37.         if (!empty($client_ip)) {
  38.                 $ip_expl = explode('.', $client_ip);
  39.                 $referer = explode('.', $remote_addr);
  40.                 if ($referer[0] != $ip_expl[0]) {
  41.                         $ip = array_reverse($ip_expl);
  42.                         $return = implode('.', $ip);
  43.                 } else {
  44.                         $return = $client_ip;
  45.                 }
  46.         } else if (!empty($x_forwarded_for)) {
  47.                 if (strstr($x_forwarded_for, ',')) {
  48.                         $ip_expl = explode(',', $x_forwarded_for);
  49.                         $return = end($ip_expl);
  50.                 } else {
  51.                         $return = $x_forwarded_for;
  52.                 }
  53.         } else {
  54.                 $return = $remote_addr;
  55.         }
  56.         unset ($client_ip, $x_forwarded_for, $remote_addr, $ip_expl);
  57.         return $return;
  58. }
  59.