Skip to content

Commit

Permalink
Add IpSupport class
Browse files Browse the repository at this point in the history
  • Loading branch information
relliv committed May 14, 2022
1 parent 54155ce commit 1d2745f
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/Support/IpSupport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace LaravelReady\UltimateSupport\Support;

use Exception;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;

class IpSupport
{
/**
* Check client is from localhost
*
* @source https://stackoverflow.com/a/21702853/6940144
*
* @return bool
*/
public static function isLocalhost(): bool
{
return in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1', '172.27.0.1']);
}

/**
* Get client public IP address if it is localhost
*
* @return null | string
*/
public static function getLocalhostPublicIp(): null | string
{
$ipAddress = null;

$response = Http::get('https://api.ipify.org/?format=json');

if ($response->ok()) {
$ipAddress = $response->json()['ip'] ?? null;

if (!empty($ipAddress)) {
return $ipAddress;
}
}

return $ipAddress;
}

/**
* Get client real IP address
*
* @source https://stackoverflow.com/q/13646690/6940144
*
* @param bool $getLocalPublicIp
*
* @return string
*/
public static function getIP(bool $getLocalPublicIp = true): string
{
$baseIp = $_SERVER['REMOTE_ADDR'];

try {
// get localhost public ip
if ($getLocalPublicIp && self::isLocalhost()) {
return self::getLocalhostPublicIp();
}

// check other conditions, cloudflare etc
if (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && filter_var($_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP)) {
return $_SERVER['HTTP_CF_CONNECTING_IP'];
} elseif (isset($_SERVER['HTTP_X_REAL_IP']) && filter_var($_SERVER['HTTP_X_REAL_IP'], FILTER_VALIDATE_IP)) {
return $_SERVER['HTTP_X_REAL_IP'];
} elseif (isset($_SERVER['HTTP_CLIENT_IP']) && filter_var($_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)) {
return $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
} catch (Exception $exp) {
Log::alert('getIP error', ['error' => $exp]);
}

return $baseIp;
}
}

0 comments on commit 1d2745f

Please sign in to comment.