0

サーバー上の特定のファイルの内容を読み取って変更する Web ページを開発しています。

これらのファイルには、1 または 0 の 2 つの値しかありません。内容の読み取り/変更は、OnChange. 基本的には、汎用入出力 (GPIO) を介してアプライアンスを制御するという考え方です。エレクトロニクス部分はすべて完了しました。Web プログラミング部分を終了する必要があり、それに行き詰まりました。

私はプログラミングの経験はまったくありませんが、あちこちで見つかったいくつかのスニペットを使用して、AJAX/PHP でその一部を実装することができました。

これまでのところ、値を読み取ることはできますが、「escapeshellarg」で正しいコマンドを作成しているにもかかわらず、値を変更することはできません。

また、ページに 2 つのインタラクティブな領域があることを期待していましたが、元の領域のみが機能しています。

誰かが私を正しい方向に向けることができますか? ヘルプ/提案/コメントは大歓迎です。


pqp6.php


<html>
<head>
    <script>
        function showUser(str)
        {
            if (str=="")
            {
                document.getElementById("txtHint").innerHTML="";
                return;
            }

            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
                }
            }

            xmlhttp.open("GET","getinfo.php?q="+str,true);
            xmlhttp.send();
        }
    </script>
</head>
<body>
    <?php                                      

        $file1="/var/www/file1";
        $file2="/var/www/file2";
        $output = shell_exec('cat '.escapeshellarg($file1));

        if($output == 0)
        {
            echo "zero ; ";
            $file1status = "off";
            $file1oposite = "on";
            $file1exec= "file1on";
        }
        else
        {
            echo "one ; ";
            $file1status= "on";
            $file1oposite= "off";
            $file1exec= "file1off";
        }

        echo "output:$output ; file1status:$file1status ; file1oposite:$file1oposite ; file1exec:$file1exec <br><br>";

        $output = shell_exec('cat '.escapeshellarg($file2));

        if($output == 0)
        {
            echo "zero ; ";
            $file2status = "off";
            $file2oposite = "on";
            $file2exec= "file2on";
        }
        else
        {
            echo "one ; ";
            $file2status= "on";
            $file2oposite= "off";
            $file2exec= "file2off";
        }

        echo "output:$output ; file2status:$file2status ; file2oposite:$file2oposite ; file2exec:$file2exec <br><br>";
        ?>

        <br>
        <br>                                    

        FILE 1:                                 
        <form>
            <select name="file1" onchange="showUser(this.value)">
                <option value="1,<?=$file1status?>"><?=$file1status?></option>
                <option value="1,<?=$file1oposite?>"><?=$file1oposite?></option>  
            </select>
        </form>
        <br>
        <div id="txtHint"><b>File 1 information will be listed here.</b>     </div>                     
        <br>
        <br>
        FILE 2:                                 
        <form>
            <select name="file2" onchange="showUser(this.value)">
                <option value="2,<?=$file2status?>"><?=$file2status?></option>
                <option value="2,<?=$file2oposite?>"><?=$file2oposite?></option>
            </select>
        </form>
        <br>
        <div id="txtHint"><b>File 2 information will be listed here.</b></div>
 </body>
</html>

getinfo.php:


<?php
$q=$_GET["q"];
$q_stripped = explode(",", $q);
$file_n = $q_stripped[0];
$file_command = $q_stripped[1];

$path="/var/www/file";

if($file_command == "on")
{
    $file_command = "1 > ";
}
else
{
    $file_command = "0 > ";
}

$command= "/bin/echo $file_command$path$file_n";
$escaped_command = escapeshellarg($command); 

echo "COMMAND: $escaped_command";
shell_exec($escaped_command);
echo "file_n=$file_n  ; file_command=$file_command ; "; 
?><?php

4

1 に答える 1