在我的页面上,您有一个开启和关闭按钮,可以在服务器上启动ssh命令。 on按钮工作正常,但是当我按下off按钮时,PHP服务器只是跳过if子句,而我的POST变量设置为false。
JS
<button id="b1" type="button" class="btn btn-primary">Start</button>
<button id="b2" type="button" class="btn btn-danger">Stop</button>
<p></p>
<script type="text/javascript">
b1 = document.getElementById('b1');
b2 = document.getElementById('b2');
b1.onclick = function () {
$.post("conJava.php", {
statusServer: "aan"
}, function (data) {
alert(data);
});
console.log("aan");
};
b2.onclick = function () {
$.post("conJava.php", {
statusServer: "uit"
}, function (data) {
alert(data);
});
console.log("uit");
};
</script>
PHP
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '/phpseclib1.0.7');
include('phpseclib1.0.7/Net/SSH2.php');
$status = $_POST['statusServer'];
echo $status;
$ssh = new Net_SSH2($_SERVER['SERVER_ADDR'] ,22);
if (!$ssh->login('pi', 'appieh4ck')) {
exit('Login Failed');
} else{
if ($status == "aan"){
$ssh->exec('pkill java');
echo "Java server is succesvol gestopt!";
}
if ($status == "uit"){
$ssh->exec('pkill java');
$ssh->exec('java -jar jserver.jar');
echo "Java server is succesvol gestart!";
}
}
?>