Issues (994)

src/shim/ip.php (2 issues)

1
<?php
2
3
/**
4
 * Detect is localhost
5
 * @see https://web-manajemen.blogspot.com/2020/10/php-detect-user-client-ip-xampp-or.html
6
 * @return boolean
7
 */
8
function isLocalHost()
9
{
10
  $whitelist = [
11
    '127.0.0.1',
12
    '::1',
13
  ];
14
15
  return in_array($_SERVER['REMOTE_ADDR'], $whitelist);
16
}
17
18
/**
19
 * Get client ip, when getenv supported (maybe cli)
20
 * @see https://web-manajemen.blogspot.com/2020/10/php-detect-user-client-ip-xampp-or.html
21
 * @return string
22
 */
23
function get_client_ip()
24
{
25
  $ipaddress = '';
26
27
  if (isLocalHost()) {
28
    $ipaddress = getLocalIp();
29
  } else {
30
    if (getenv('HTTP_CLIENT_IP')) {
31
      $ipaddress = getenv('HTTP_CLIENT_IP');
32
    } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
33
      $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
34
    } elseif (getenv('HTTP_X_FORWARDED')) {
35
      $ipaddress = getenv('HTTP_X_FORWARDED');
36
    } elseif (getenv('HTTP_FORWARDED_FOR')) {
37
      $ipaddress = getenv('HTTP_FORWARDED_FOR');
38
    } elseif (getenv('HTTP_FORWARDED')) {
39
      $ipaddress = getenv('HTTP_FORWARDED');
40
    } elseif (getenv('REMOTE_ADDR')) {
41
      $ipaddress = $ipaddress = getenv('REMOTE_ADDR');
0 ignored issues
show
The assignment to $ipaddress is dead and can be removed.
Loading history...
42
    } else {
43
      /**
44
       * Return to method 2
45
       */
46
      $ipaddress = get_client_ip2();
0 ignored issues
show
Are you sure the assignment to $ipaddress is correct as get_client_ip2() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
47
    }
48
  }
49
50
  return $ipaddress;
51
}
52
53
/**
54
 * Get client ip, when running on webserver
55
 * @see https://web-manajemen.blogspot.com/2020/10/php-detect-user-client-ip-xampp-or.html
56
 * @return void
57
 */
58
function get_client_ip2()
59
{
60
  $ipaddress = '';
61
  if (isLocalHost()) {
62
    $ipaddress = getLocalIp();
63
  } else {
64
    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
65
      $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
66
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
67
      $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
68
    } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
69
      $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
70
    } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
71
      $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
72
    } elseif (isset($_SERVER['HTTP_FORWARDED'])) {
73
      $ipaddress = $_SERVER['HTTP_FORWARDED'];
74
    } elseif (isset($_SERVER['REMOTE_ADDR'])) {
75
      $ipaddress = $_SERVER['REMOTE_ADDR'];
76
    } else {
77
      $ipaddress = 'UNKNOWN';
78
    }
79
  }
80
81
  return $ipaddress;
82
}
83
84
/**
85
 * Get localhost ip
86
 * @see https://web-manajemen.blogspot.com/2020/10/php-detect-user-client-ip-xampp-or.html
87
 * @return string
88
 */
89
function getLocalIp()
90
{
91
  if (defined('PHP_MAJOR_VERSION') && PHP_MAJOR_VERSION >= 5) {
92
    $localIP = gethostbyname(gethostname());
93
  } else {
94
    $localIP = gethostbyname(php_uname('n'));
95
  }
96
97
  return $localIP;
98
}
99