Created
August 12, 2021 08:07
-
-
Save abdullahnaseer/b6dc604e841902e85a9bafda2b80f92d to your computer and use it in GitHub Desktop.
IP Function for Google Cloud Function (For Purpose of Upwork Test Only)
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
<?php | |
use GuzzleHttp\Psr7\Response; | |
use Psr\Http\Message\ServerRequestInterface; | |
function get_ip(ServerRequestInterface $request) | |
{ | |
$data = ["ip" => get_client_ip()]; | |
$queryString = $request->getQueryParams(); | |
if (isset($queryString['name'])) { | |
$data['greeting'] = "Hello, " . $queryString['name'] . "!"; | |
} | |
return new Response( | |
200, | |
[ | |
'Content-Type' => 'application/json', | |
'x-hello-world' => 'Muhammad A.' | |
], | |
json_encode($data) | |
); | |
} | |
// Function to get the client IP address | |
function get_client_ip() | |
{ | |
$ipaddress = ''; | |
if (isset($_SERVER['HTTP_CLIENT_IP'])) | |
$ipaddress = $_SERVER['HTTP_CLIENT_IP']; | |
else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) | |
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
else if (isset($_SERVER['HTTP_X_FORWARDED'])) | |
$ipaddress = $_SERVER['HTTP_X_FORWARDED']; | |
else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) | |
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; | |
else if (isset($_SERVER['HTTP_FORWARDED'])) | |
$ipaddress = $_SERVER['HTTP_FORWARDED']; | |
else if (isset($_SERVER['REMOTE_ADDR'])) | |
$ipaddress = $_SERVER['REMOTE_ADDR']; | |
else | |
$ipaddress = 'UNKNOWN'; | |
return $ipaddress; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment