PHP 代码似乎跳过了“if”语句

问题描述 投票:0回答:2

我熟悉编写 bash 脚本,但 PHP 对我来说是新的。

我有以下代码,它是使用从 HTML 表单加载的变量来执行的(也是上周学到的),如果我输入域名来对其进行查找,则该代码可以工作,但是,如果表单尝试执行仅针对 IP 地址进行查找,这是行不通的。它似乎跳过了代码的整个“else”部分。我做错了什么?

<?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";
?>

名称查找有效,但 IP 地址部分被完全跳过。

php scripting
2个回答
0
投票

$text1 可能不会评估为空。在 if 块之间回显 $text1 和 $text2,以便您可以确认这一点。如果是这样,请将 if (!empty($text1)) 更改为其他内容,以适应您将收到的输入值。


0
投票

那是因为你给出了一个 if 语句 - 如果 $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;

否则 $text1 为空,那么您的 IP 地址部分将起作用。

© www.soinside.com 2019 - 2024. All rights reserved.