Skip to content

Commit 0560541

Browse files
author
pulkit
committed
added random option to return random ipaddress for use on homestead
1 parent 4111484 commit 0560541

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/GeoIP.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class GeoIP
1616
*/
1717
protected $driver;
1818

19+
/**
20+
* @var boolean
21+
*/
22+
protected $random;
23+
1924
/**
2025
* @var array
2126
*/
@@ -27,6 +32,7 @@ class GeoIP
2732
public function __construct(array $config = ['driver' => 'ip-api'])
2833
{
2934
$this->driver = with(new GeoIPManager($config))->getDriver();
35+
$this->random = array_get($config, 'randon', false);
3036
}
3137

3238
/**
@@ -60,7 +66,15 @@ public function setIp($ip)
6066
*/
6167
public function getIp()
6268
{
63-
return ($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'))))));
69+
if ($this->ip) {
70+
return $this->ip;
71+
}
72+
73+
if ($this->random) {
74+
return long2ip(rand(0, '4294967295'));
75+
}
76+
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'))))));
6478
}
6579

6680
/**
@@ -92,6 +106,7 @@ public function get($property = '')
92106
protected function getData()
93107
{
94108
$ip = $this->getIp();
109+
$this->setIp($ip);
95110

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

src/config/config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
*/
1212
'driver' => 'ip-api',
1313

14+
/*
15+
|--------------------------------------------------------------------------
16+
| Return random ipaddresses (useful for dev envs)
17+
|--------------------------------------------------------------------------
18+
*/
19+
'random' => false,
20+
1421
/*
1522
|--------------------------------------------------------------------------
1623
| IP-API Driver

0 commit comments

Comments
 (0)