Skip to content

Commit 3c94f75

Browse files
author
Soban Mahmod
committed
added option for originating client ip, minor refactoring
1 parent a41851b commit 3c94f75

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

src/iphandler/DefaultIPHandler.php renamed to src/iphandler/DefaultIPSelector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
namespace ipinfo\ipinfolaravel\iphandler;
44

55
/**
6-
* Default implementation of the IPHandlerInterface. Selects default ip from request.
6+
* Implementation of the IPHandlerInterface used as default option. Retrieve IP from request.
77
*/
8-
class DefaultIPHandler implements IPHandlerInterface
8+
class DefaultIPSelector implements IPHandlerInterface
99
{
1010

1111
/**
12-
* Selectes default IP address from request.
12+
* Selects default IP address from request.
1313
* @param \Illuminate\Http\Request $request
1414
* @return string IP address.
1515
*/

src/iphandler/IPHandlerInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace ipinfo\ipinfolaravel\iphandler;
44

55
/**
6-
* Interface for handling the mechanism for IP retrieval.
6+
* Interface for handling the mechanism of IP retrieval.
77
*/
88
interface IPHandlerInterface
99
{
10-
10+
1111
/**
1212
* Get IP address.
1313
* @param \Illuminate\Http\Request $request
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace ipinfo\ipinfolaravel\iphandler;
4+
5+
/**
6+
* Selects originating client IP from the request.
7+
*/
8+
class OriginatingIPSelector implements IPHandlerInterface
9+
{
10+
11+
/**
12+
* Selects originating client IP from request.
13+
* @param \Illuminate\Http\Request $request
14+
* @return string IP address.
15+
*/
16+
public function getIP($request)
17+
{
18+
$xForwardedFor = $request->headers->get('x-forwarded-for');
19+
if (empty($xForwardedFor)) {
20+
$ip = $request->ip();
21+
} else {
22+
$ips = explode(',', $xForwardedFor);
23+
// trim as officially the space comes after each comma separator
24+
$ip = trim($ips[0]);
25+
}
26+
return $ip;
27+
}
28+
}

src/ipinfolaravel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Closure;
66
use ipinfo\ipinfo\IPinfo as IPinfoClient;
77
use ipinfo\ipinfolaravel\DefaultCache;
8-
use ipinfo\ipinfolaravel\iphandler\DefaultIPHandler;
8+
use ipinfo\ipinfolaravel\iphandler\DefaultIPSelector;
99

1010
class ipinfolaravel
1111
{
@@ -77,7 +77,7 @@ public function configure()
7777
$this->access_token = config('services.ipinfo.access_token', null);
7878
$this->filter = config('services.ipinfo.filter', [$this, 'defaultFilter']);
7979
$this->no_except = config('services.ipinfo.no_except', false);
80-
$this->ip_selector = config('services.ipinfo.ip_selector', new DefaultIPHandler());
80+
$this->ip_selector = config('services.ipinfo.ip_selector', new DefaultIPSelector());
8181

8282
if ($custom_countries = config('services.ipinfo.countries_file', null)) {
8383
$this->settings['countries_file'] = $custom_countries;

0 commit comments

Comments
 (0)