Skip to content

Instantly share code, notes, and snippets.

@abdullahnaseer
Created August 12, 2021 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdullahnaseer/b6dc604e841902e85a9bafda2b80f92d to your computer and use it in GitHub Desktop.
Save abdullahnaseer/b6dc604e841902e85a9bafda2b80f92d to your computer and use it in GitHub Desktop.
IP Function for Google Cloud Function (For Purpose of Upwork Test Only)
<?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