skip to Main Content

I am familiar with writing bash scripts, but PHP is new to me.

I have the following code which is executed with variables loaded from a HTML form (also learned in the last week), and the code works if I enter a domain name to do lookup against it, however, if the form tried to do a lookup against an IP address only, it doesn’t work. It seems to skip the entire "else" part of the code. What am I doing wrong?

<?php
header('Content-type: text/plain');
if(isset($_GET['subject1'], $_GET['subject2'])) {

    $text1 = $_GET['subject1'];
    $text2 = $_GET['subject2'];

    $text1 = escapeshellarg($text1);
    $text2 = escapeshellarg($text2);
    echo "THE VARIABLES THAT YOU HAVE SUBMITTED ARE "  . "n";
    echo "You entered $text1 for the Domain Name "  . "n";
    echo "You entered $text2 for the IP Address "  . "n";
    echo "n";
}

if (!empty($text1)) {
    $command1 = '/usr/bin/dig ' . '@localhost ' . "$text1 " . "+short " . "| /usr/bin/tail -n1" . "n";
    echo "n";
    echo "THE COMMAND THAT YOU HAVE SUBMITTED WILL THEREFORE BE "  . "n";
    print_r($command1) . "n";
    echo "n";
    $output1 = shell_exec($command1);
    echo "n";
    echo "RESULTS OF A EXTERNAL DNS (NAME TO IP) LOOKUP FOR THE DOMAIN ENTERED" . "n";
    echo "$output1";
    echo "n";

    $command2 = '/usr/bin/whois ' . "$text1" . "n";
    echo "n";
    $output2 = shell_exec($command2);
    echo "n";
    echo "RESULTS OF A WHOIS LOOKUP AGAINST " . "$text1 " . "n";
    echo "$output2";
    echo "n";

    $command3 = '/usr/bin/whois ' . "$output1" . "n";
    echo "n";
    $output3 = shell_exec($command3);
    echo "n";
    echo "RESULTS OF A WHOIS LOOKUP AGAINST " . "$output1 " . "n";
    echo "$output3";
    echo "n";
    goto END;

} else {

    $command4 = '/usr/bin/whois ' . "$text2" . "n";
    echo "n";
    $output4 = shell_exec($command4);
    echo "n";
    echo "RESULTS OF A WHOIS LOOKUP AGAINST " . "$text2 " . "n";
    echo "$output4";
    echo "n";
    goto END;
}
END:
echo "END OF THE TEST RESULTS " . "n";
?>

The name lookups work, but the ip address section is skipped in its entirety.

2

Answers


  1. Probably $text1 does not evalue to empty. Echo $text1 and $text2 between your if blocks, so you could confirm that. If it’s that, change the if (!empty($text1)) for something else which adjusts to the input values you’ll receive.

    Login or Signup to reply.
  2. That is because you give an if statement –
    if $text1 is not empty it will do this

    $command1 = '/usr/bin/dig ' . '@localhost ' . "$text1 " . "+short " . "| /usr/bin/tail -n1" . "n";
        echo "n";
        echo "THE COMMAND THAT YOU HAVE SUBMITTED WILL THEREFORE BE "  . "n";
        print_r($command1) . "n";
        echo "n";
        $output1 = shell_exec($command1);
        echo "n";
        echo "RESULTS OF A EXTERNAL DNS (NAME TO IP) LOOKUP FOR THE DOMAIN ENTERED" . "n";
        echo "$output1";
        echo "n";
    
        $command2 = '/usr/bin/whois ' . "$text1" . "n";
        echo "n";
        $output2 = shell_exec($command2);
        echo "n";
        echo "RESULTS OF A WHOIS LOOKUP AGAINST " . "$text1 " . "n";
        echo "$output2";
        echo "n";
    
        $command3 = '/usr/bin/whois ' . "$output1" . "n";
        echo "n";
        $output3 = shell_exec($command3);
        echo "n";
        echo "RESULTS OF A WHOIS LOOKUP AGAINST " . "$output1 " . "n";
        echo "$output3";
        echo "n";
        goto END;
    

    and else $text1 is empty then your IP address section will work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search