VPN/Proxy server Detection with IPHub API and PHP

If you are managing content based on the country then there is always a chance that a user tries to access other country content using VPN or Proxy.

If you are not validating it then the user can access the content.

In this tutorial, I show how you can detect VPN or Proxy server with IPHub API.

I am using PHP to get an IP address and sending an API request.

VPN/Proxy server detection with IPHub API and PHP


Contents

  1. Get API Key
  2. Get IP
  3. Check VPN and Proxy
  4. Output
  5. Conclusion

1. Get API Key

  • Create an account in IPHub and verify your account.

VPN/Proxy server detection with IPHub API and PHP

  • Click on the link under Your API keys.

VPN/Proxy server detection with IPHub API and PHP

  • Purchase a plan or select the free plan for testing. That allows sending 1000 requests per day.

VPN/Proxy server detection with IPHub API and PHP

  • Copy the API key.

2. Get IP

Create get_client_ip() function to get the IP address.

Completed Code

// Get IP address
function get_client_ip() {
   $ipaddress = '';
   if (getenv('HTTP_CLIENT_IP'))
      $ipaddress = getenv('HTTP_CLIENT_IP');
   else if(getenv('HTTP_X_FORWARDED_FOR'))
      $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
   else if(getenv('HTTP_X_FORWARDED'))
      $ipaddress = getenv('HTTP_X_FORWARDED');
   else if(getenv('HTTP_FORWARDED_FOR'))
      $ipaddress = getenv('HTTP_FORWARDED_FOR');
   else if(getenv('HTTP_FORWARDED'))
      $ipaddress = getenv('HTTP_FORWARDED');
   else if(getenv('REMOTE_ADDR'))
      $ipaddress = getenv('REMOTE_ADDR');
   else
      $ipaddress = 'UNKNOWN';
   return $ipaddress;
}

3. Check VPN and Proxy

Get IP address by calling get_client_ip() and assign to $ipaddress.

Send a cURL GET request to "http://v2.api.iphub.info/ip/".$ipaddress. Pass API key with the header.

Assign cURL response to $response. API returns following response –

stdClass Object
(
    [ip] => 8.8.8.8
    [countryCode] => IN
    [countryName] => India
    [asn] => 15169
    [isp] => RELIANCEJIO-IN
    [block] => 0
    [hostname] => 8.8.8.8
)

Read block value from $response and assign to $block variable.

If $block == 1 means user is using VPN or Proxy.

Completed Code

## Check VPN and Proxy
$ipaddress = get_client_ip(); // Get IP
$apikey = "YI51XTM6Z2JUa1RlRk9EbW1kUXpDdE1qamh6MGlpUzFlT0pXT0c="; // API Key

// cURL request
$curl = curl_init();

curl_setopt_array($curl, array(
   CURLOPT_URL => "http://v2.api.iphub.info/ip/".$ipaddress,
   CURLOPT_RETURNTRANSFER => true,
   CURLOPT_TIMEOUT => 30,
   CURLOPT_CUSTOMREQUEST => "GET",
   CURLOPT_HTTPHEADER => array(
      "x-key: ".$apikey
   ),
));

$response = json_decode(curl_exec($curl));
$err = curl_error($curl);

curl_close($curl);
$block = 0;
if ($err) {
   echo "cURL Error :" . $err;
} else {
   $block = $response->block;
}

if($block == 1){
   echo "Using VPN or Proxy.";
}else{
   echo "Not using VPN or Proxy.";
}

echo "<pre>";
print_r($response);
echo "</pre>";

4. Output

View Demo


5. Conclusion

IPHub allows sending 1000 requests per day. Buy a plan if more than 1000 requests send from your website.

Perform your action if API returns block value is 1 like – stop the user from accessing the website or redirect with a warning message to disable VPN or Proxy server.

If you found this tutorial helpful then don't forget to share.

Leave a Comment