skip to Main Content

Im checking users on my website if they use vpn or not with php connected via API, my script working good, if user used vpn an status says : "is a VPN"

if user not using vpn than status says : "is not a VPN"

what i wanted in my script, is that if user do not using vpn whole page of HTML should displayed not a status say : "is not a VPN" and than if using vpn the status says : "is a VPN" normally

<?php
header('Content-type: text/plain');

$IP_ADDRESS = '127.0.0.1'; # Manual IP Address
$API_KEY = "";
$API_URL = 'https://vpnapi.io/api/' . $IP_ADDRESS . '?key=' . $API_KEY;

$response = file_get_contents($API_URL);
$response = json_decode($response);

if(!$response->security->vpn) {
    echo " is a VPN.n";
} else {
    echo " is not a VPN.n";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="content-type">
  <title></title>
</head>
<body>
<div style="text-align: center; color: rgb(51, 204, 0);"><big
 style="font-weight: bold;"><big><big><br>
Test Working!!!</big></big></big></div>
</body>
</html>

2

Answers


  1. Just as your code already does… Wrap what you want to happen in the if and else blocks so that it’s simply within those blocks:

    if($response->security->vpn) {
        echo " is a VPN.n";
    } else {
    
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta content="text/html; charset=ISO-8859-1"
     http-equiv="content-type">
      <title></title>
    </head>
    <body>
    <div style="text-align: center; color: rgb(51, 204, 0);"><big
     style="font-weight: bold;"><big><big><br>
    Test Working!!!</big></big></big></div>
    </body>
    </html>
    
    <?php } ?>
    

    Alternatively, as a matter of preference and readability, you can remove the else entirely and just conditionally return to end the script. For example:

    if($response->security->vpn) {
        echo " is a VPN.n";
        return;
    }
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta content="text/html; charset=ISO-8859-1"
     http-equiv="content-type">
      <title></title>
    </head>
    <body>
    <div style="text-align: center; color: rgb(51, 204, 0);"><big
     style="font-weight: bold;"><big><big><br>
    Test Working!!!</big></big></big></div>
    </body>
    </html>
    

    Unless I’m misunderstanding and you want the if condition to show the message and the page, while the else only shows the page? If that’s the case then what you have is simply a basic if condition to conditionally perform an operation:

    if($response->security->vpn) {
        echo " is a VPN.n";
    }
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta content="text/html; charset=ISO-8859-1"
     http-equiv="content-type">
      <title></title>
    </head>
    <body>
    <div style="text-align: center; color: rgb(51, 204, 0);"><big
     style="font-weight: bold;"><big><big><br>
    Test Working!!!</big></big></big></div>
    </body>
    </html>
    

    Any way you look at it, the structure is always the same. Operations that are conditionally performed (that is, only sometimes performed depending on some data) go in if or else blocks.

    Login or Signup to reply.
  2. A fairly simple change

    <?php
    header('Content-type: text/plain');
    
    $IP_ADDRESS = '127.0.0.1'; # Manual IP Address
    $API_KEY = "";
    $API_URL = 'https://vpnapi.io/api/' . $IP_ADDRESS . '?key=' . $API_KEY;
    
    $response = file_get_contents($API_URL);
    $response = json_decode($response);
    
    if(!$response->security->vpn) {
        echo " is a VPN.n";
    } else {
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta content="text/html; charset=ISO-8859-1"
     http-equiv="content-type">
      <title></title>
    </head>
    <body>
    <div style="text-align: center; color: rgb(51, 204, 0);"><big
     style="font-weight: bold;"><big><big><br>
    Test Working!!!</big></big></big></div>
    </body>
    </html>
    }
    ?>
    

    Alternatively you could output the message as part of the page

    <?php
    header('Content-type: text/plain');
    
    $IP_ADDRESS = '127.0.0.1'; # Manual IP Address
    $API_KEY = "";
    $API_URL = 'https://vpnapi.io/api/' . $IP_ADDRESS . '?key=' . $API_KEY;
    
    $response = file_get_contents($API_URL);
    $response = json_decode($response);
    $msg = '';
    if(!$response->security->vpn) {
        $msg = " is a VPN.";
    } else {
        $msg = 'is not a VPN.';
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta content="text/html; charset=ISO-8859-1"
     http-equiv="content-type">
      <title></title>
    </head>
    <body>
    <div style="text-align: center; color: rgb(51, 204, 0);">
        <strong><?php echo $msg; ?></strong>
    </div>
    </body>
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search