Skip to content

Commit d481b3c

Browse files
author
pulkit
committed
added test and fixed
1 parent 78def95 commit d481b3c

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/GeoIP.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ public function getIp()
7070
if ($this->random) {
7171
$this->ip = long2ip(rand(0, '4294967295'));
7272
} else {
73-
$ip = array_get($_SERVER, 'HTTP_CLIENT_IP', array_get($_SERVER, 'HTTP_X_FORWARDED_FOR', array_get($_SERVER, 'HTTP_X_FORWARDED', array_get($_SERVER, 'HTTP_FORWARDED_FOR', array_get($_SERVER, 'HTTP_FORWARDED', array_get($_SERVER, 'REMOTE_ADDR', '127.0.0.1'))))));
74-
75-
// sometimes we can get multiple ipaddresses
76-
// seperated with a ',', eg: proxy/vpn
77-
$ip = explode(',', $ip);
78-
$this->ip = trim(head($ip));
73+
$this->ip = array_get($_SERVER, 'HTTP_CLIENT_IP', array_get($_SERVER, 'HTTP_X_FORWARDED_FOR', array_get($_SERVER, 'HTTP_X_FORWARDED', array_get($_SERVER, 'HTTP_FORWARDED_FOR', array_get($_SERVER, 'HTTP_FORWARDED', array_get($_SERVER, 'REMOTE_ADDR', '127.0.0.1'))))));
7974
}
8075
}
8176

77+
// sometimes we can get multiple ipaddresses
78+
// seperated with a ',', eg: proxy/vpn
79+
$this->ip = explode(',', $this->ip);
80+
$this->ip = trim(head($this->ip));
81+
8282
return $this->ip;
8383
}
8484

@@ -110,8 +110,8 @@ public function get($property = '')
110110
*/
111111
protected function getData()
112112
{
113-
$ips = $this->getIp();
114-
$this->setIp($ips);
113+
$ip = $this->getIp();
114+
$this->setIp($ip);
115115

116116
// check ip in memory
117117
$data = array_get($this->store, $ip);

tests/GeoIPTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class GeoIPTest extends PHPUnit_Framework_TestCase
99
{
10+
protected $multipleIps = '81.2.69.160,127.0.0.1';
1011
protected $validIp = '81.2.69.160';
1112
protected $invalidIp = '127.0.0.1';
1213

@@ -126,6 +127,20 @@ public function test_get_non_random_ipaddress()
126127
$this->assertTrue(!(filter_var($ip, FILTER_VALIDATE_IP)) === False);
127128
}
128129

130+
public function test_get_multiple_ipaddress()
131+
{
132+
$config = [
133+
'driver' => 'ip-api',
134+
];
135+
136+
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
137+
$geoip->setIp($this->multipleIps);
138+
$ip = $geoip->getIp();
139+
140+
$this->assertEquals($ip, $this->validIp);
141+
$this->assertTrue(!(filter_var($ip, FILTER_VALIDATE_IP)) === False);
142+
}
143+
129144
public function test_maxmind_database()
130145
{
131146
$config = [

0 commit comments

Comments
 (0)