Skip to content

Commit 7011b0e

Browse files
author
pulkit
committed
merged in 1
2 parents 4cad701 + ef2376e commit 7011b0e

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

src/GeoIP.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ public function setIp($ip)
6666
*/
6767
public function getIp()
6868
{
69-
if ($this->ip) {
70-
return $this->ip;
71-
}
72-
73-
if ($this->random) {
74-
return long2ip(rand(0, '4294967295'));
69+
if (!$this->ip) {
70+
if ($this->random) {
71+
$this->ip = long2ip(rand(0, '4294967295'));
72+
} else {
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'))))));
74+
}
7575
}
7676

77-
return 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'))))));
77+
return $this->ip;
7878
}
7979

8080
/**
@@ -106,6 +106,7 @@ public function get($property = '')
106106
protected function getData()
107107
{
108108
$ip = $this->getIp();
109+
$this->setIp($ip);
109110

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

tests/GeoIPTest.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
class GeoIPTest extends PHPUnit_Framework_TestCase
99
{
10-
protected $validIP = '81.2.69.160';
11-
protected $invalidIP = '127.0.0.1';
10+
protected $validIp = '81.2.69.160';
11+
protected $invalidIp = '127.0.0.1';
1212

1313
public function tearDown()
1414
{
@@ -93,11 +93,25 @@ public function test_maxmind_web_api_authentication_exception()
9393
$this->setExpectedException('PulkitJalan\GeoIP\Exceptions\GeoIPException');
9494

9595
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
96-
$geoip = $geoip->setIP($this->validIP);
96+
$geoip = $geoip->setIp($this->validIp);
9797

9898
$geoip->get();
9999
}
100100

101+
public function test_get_random_ipaddress()
102+
{
103+
$config = [
104+
'driver' => 'ip-api',
105+
'random' => true,
106+
];
107+
108+
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
109+
$ip = $geoip->getIp();
110+
111+
$this->assertNotEquals($ip, $this->invalidIp);
112+
$this->assertTrue(!(filter_var($ip, FILTER_VALIDATE_IP)) === False);
113+
}
114+
101115
public function test_maxmind_database()
102116
{
103117
$config = [
@@ -108,11 +122,11 @@ public function test_maxmind_database()
108122
];
109123

110124
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
111-
$geoip = $geoip->setIP($this->validIP);
125+
$geoip = $geoip->setIp($this->validIp);
112126

113127
$this->assertEquals($geoip->getCountry(), 'United Kingdom');
114128

115-
$geoip = $geoip->setIP($this->invalidIP);
129+
$geoip = $geoip->setIp($this->invalidIp);
116130

117131
$this->assertEquals($geoip->get(), []);
118132
$this->assertEquals($geoip->getCountry(), '');
@@ -131,7 +145,7 @@ public function test_ip_api_pro_exception()
131145
$this->setExpectedException('PulkitJalan\GeoIP\Exceptions\GeoIPException');
132146

133147
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
134-
$geoip = $geoip->setIP($this->validIP);
148+
$geoip = $geoip->setIp($this->validIp);
135149

136150
$geoip->get();
137151
}
@@ -143,11 +157,11 @@ public function test_ip_api()
143157
];
144158

145159
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
146-
$geoip = $geoip->setIP($this->validIP);
160+
$geoip = $geoip->setIp($this->validIp);
147161

148162
$this->assertEquals($geoip->getCountry(), 'United Kingdom');
149163

150-
$geoip = $geoip->setIP($this->invalidIP);
164+
$geoip = $geoip->setIp($this->invalidIp);
151165

152166
$this->assertEquals($geoip->get(), []);
153167
$this->assertEquals($geoip->getCountry(), '');
@@ -160,11 +174,11 @@ public function test_telize()
160174
];
161175

162176
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
163-
$geoip = $geoip->setIP($this->validIP);
177+
$geoip = $geoip->setIp($this->validIp);
164178

165179
$this->assertEquals($geoip->getCountry(), 'United Kingdom');
166180

167-
$geoip = $geoip->setIP($this->invalidIP);
181+
$geoip = $geoip->setIp($this->invalidIp);
168182

169183
$this->assertEquals($geoip->get(), []);
170184
$this->assertEquals($geoip->getCountry(), '');
@@ -180,11 +194,11 @@ public function test_telize_secure()
180194
];
181195

182196
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
183-
$geoip = $geoip->setIP($this->validIP);
197+
$geoip = $geoip->setIp($this->validIp);
184198

185199
$this->assertEquals($geoip->getCountry(), 'United Kingdom');
186200

187-
$geoip = $geoip->setIP($this->invalidIP);
201+
$geoip = $geoip->setIp($this->invalidIp);
188202

189203
$this->assertEquals($geoip->get(), []);
190204
$this->assertEquals($geoip->getCountry(), '');

0 commit comments

Comments
 (0)