doushai4890 2018-05-14 04:07
浏览 66

PHP脚本不起作用

Basically, I want to run below script locally in order start stop restart and get status of service. And it must show output of each command button. I am using php 5.6 in order to run this code.

Below is the code:

<?php
    // define cmds
    $commands = [
    'stop_apache' => [
        'description' => 'Stop Apache2',
        'cmd' => 'systemctl stop apache2'
    ],
    'restart_apache' => [
        'description' => 'Restart Apache2',
        'cmd' => 'systemctl restart apache2'
    ],
    'start_apache' => [
        'description' => 'Start Apache2',
        'cmd' => 'systemctl start apache2'
    ],
    'status_apache' => [
        'description' => 'Status Apache2',
        'cmd' => 'systemctl status apache2'
    ],
    ];

    // handle post
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $error = [];
    $result = '';

    // validate input
    if (empty($_POST['service'])) {
        $error = [
            'service' => 'Service type required!'    
        ];
    } elseif (!array_key_exists($_POST['service'], $commands)) {
        $error = [
            'service' => 'Invalid Service!'    
        ];
    }
    }
    ?>
    <form action="" method="post">
    <?php if (!empty($error)): ?>
    <h3>Error</h3>
    <pre><?= print_r($error, true) ?></pre>
    <?php endif ?>
    <?php foreach ($commands as $key => $command): ?>
    <button type="submit" name="service" value="<?= $key ?>"><?= 
    $command['description'] ?></button>
    <?php endforeach ?>
    </form>
   <?php if (!empty($result)): ?>
   <pre><?= print_r($result, true) ?></pre>
   <?php endif ?>
  • 写回答

2条回答 默认 最新

  • douzhang1926 2018-05-14 04:21
    关注
    // handle post
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $error = [];
    $result = '';
    
    // validate input
    if (empty($_POST['service'])) {
        $error = [
            'service' => 'Service type required!'    
        ];
    } elseif (!array_key_exists($_POST['service'], $commands)) {
        $error = [
            'service' => 'Invalid Service!'    
        ];
    }
    //you need add this line
    else $result = $commands[$_POST['service']];
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号