-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from ipinfo/soban/ip-selection-handler
Add optional IP selection handler
- Loading branch information
Showing
5 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace ipinfo\ipinfolaravel\iphandler; | ||
|
||
/** | ||
* Implementation of the IPHandlerInterface used as default option. Retrieve IP from request. | ||
*/ | ||
class DefaultIPSelector implements IPHandlerInterface | ||
{ | ||
|
||
/** | ||
* Selects default IP address from request. | ||
* @param \Illuminate\Http\Request $request | ||
* @return string IP address. | ||
*/ | ||
public function getIP($request) | ||
{ | ||
return $request->ip(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace ipinfo\ipinfolaravel\iphandler; | ||
|
||
/** | ||
* Interface for handling the mechanism of IP retrieval. | ||
*/ | ||
interface IPHandlerInterface | ||
{ | ||
|
||
/** | ||
* Get IP address. | ||
* @param \Illuminate\Http\Request $request | ||
* @return string IP address. | ||
*/ | ||
public function getIP($request); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace ipinfo\ipinfolaravel\iphandler; | ||
|
||
/** | ||
* Selects originating client IP from the request. | ||
*/ | ||
class OriginatingIPSelector implements IPHandlerInterface | ||
{ | ||
|
||
/** | ||
* Selects originating client IP from request. | ||
* @param \Illuminate\Http\Request $request | ||
* @return string IP address. | ||
*/ | ||
public function getIP($request) | ||
{ | ||
$xForwardedFor = $request->headers->get('x-forwarded-for'); | ||
if (empty($xForwardedFor)) { | ||
$ip = $request->ip(); | ||
} else { | ||
$ips = explode(',', $xForwardedFor); | ||
// trim as officially the space comes after each comma separator | ||
$ip = trim($ips[0]); | ||
} | ||
return $ip; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters