将字符串发送到 php 服务器并使用它
Send string to php server and use it
我试图将字符串发送到 php 服务器,但由于某种原因,我无法读取服务器上的字符串...我尝试了很多方法来正确输入它,但看起来我从来没有得到正确的语法。有人有线索吗?
var command="";
if(document.getElementById("Text_1").value != "" && document.getElementById("Text_2").value != "")
{
command += " " + document.getElementById("Text_1").value + " " + document.getElementById("Text_2").value;
}
alert(command);
xmlhttp.open("POST", "server.php", false);
xmlhttp.setRequestHeader('info', command)
//TRIED xmlhttp.setRequestHeader("info, command")
//TRIED xmlhttp.setRequestHeader('info', 'command')
//TRIED many others sketchy things...
xmlhttp.send();
//TRIED xmlhttp.send(command);
var output = xmlhttp.responseText;
在 php 服务器上:
<?php
$parameter = $_POST['command'];
$output = exec("someexecutable.exe $parameter");
echo json_encode($parameter);
?>
他们想知道,如果我用正确的字符串对 $parameter 进行硬编码,它就可以工作,所以可执行文件不是问题。服务器无法获取 $_POST 中字符串的值。
setRequestHeader
用于在请求上设置headers。 Content-type
和 Content-length
.
之类的东西
您需要将数据传递给send()
。要使 $_POST
起作用,它们需要采用 key=val&vey2=val2
格式。实际上,在较新的浏览器中,您可以使用 FormData
.
xmlhttp.open("POST", "server.php", false);
// To emulate a `<form>` POST
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// To get the response, you need to set a callback
xmlhttp.onreadystatechange = function(){
// readyState 4 = complete
// status = 200 OK
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
var output = xmlhttp.responseText;
}
};
// Create the Form Data
var params = new FormData;
params.append('command', command);
xmlhttp.send(params);
P.S。你应该 运行 escapeshellarg()
在 运行 之前执行你的命令。如果人们可以在您的服务器上 运行 任意命令,这可能比 SQL 注入更糟糕。
<?php
$parameter = escapeshellarg($_POST['command']);
$output = exec("someexecutable.exe $parameter");
?>
P.P.S。 escapeshellarg()
将使您的命令将 entire $_POST['command']
字符串视为 one 参数。如果你不想要那个,那么你需要从你的 JavaScript.
POST 一个 array
// Create the Form Data
var params = new FormData;
params.append('command[]', document.getElementById("Text_1").value);
params.append('command[]', document.getElementById("Text_2").value);
xmlhttp.send(params);
现在 $_POST['command']
将是一个数组,因此您必须 运行 命令如下:
<?php
$parameters = array_map('escapeshellarg', $_POST['command']);
$output = exec("someexecutable.exe ".implode(' ', $parameters));
?>
我试图将字符串发送到 php 服务器,但由于某种原因,我无法读取服务器上的字符串...我尝试了很多方法来正确输入它,但看起来我从来没有得到正确的语法。有人有线索吗?
var command="";
if(document.getElementById("Text_1").value != "" && document.getElementById("Text_2").value != "")
{
command += " " + document.getElementById("Text_1").value + " " + document.getElementById("Text_2").value;
}
alert(command);
xmlhttp.open("POST", "server.php", false);
xmlhttp.setRequestHeader('info', command)
//TRIED xmlhttp.setRequestHeader("info, command")
//TRIED xmlhttp.setRequestHeader('info', 'command')
//TRIED many others sketchy things...
xmlhttp.send();
//TRIED xmlhttp.send(command);
var output = xmlhttp.responseText;
在 php 服务器上:
<?php
$parameter = $_POST['command'];
$output = exec("someexecutable.exe $parameter");
echo json_encode($parameter);
?>
他们想知道,如果我用正确的字符串对 $parameter 进行硬编码,它就可以工作,所以可执行文件不是问题。服务器无法获取 $_POST 中字符串的值。
setRequestHeader
用于在请求上设置headers。 Content-type
和 Content-length
.
您需要将数据传递给send()
。要使 $_POST
起作用,它们需要采用 key=val&vey2=val2
格式。实际上,在较新的浏览器中,您可以使用 FormData
.
xmlhttp.open("POST", "server.php", false);
// To emulate a `<form>` POST
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// To get the response, you need to set a callback
xmlhttp.onreadystatechange = function(){
// readyState 4 = complete
// status = 200 OK
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
var output = xmlhttp.responseText;
}
};
// Create the Form Data
var params = new FormData;
params.append('command', command);
xmlhttp.send(params);
P.S。你应该 运行 escapeshellarg()
在 运行 之前执行你的命令。如果人们可以在您的服务器上 运行 任意命令,这可能比 SQL 注入更糟糕。
<?php
$parameter = escapeshellarg($_POST['command']);
$output = exec("someexecutable.exe $parameter");
?>
P.P.S。 escapeshellarg()
将使您的命令将 entire $_POST['command']
字符串视为 one 参数。如果你不想要那个,那么你需要从你的 JavaScript.
// Create the Form Data
var params = new FormData;
params.append('command[]', document.getElementById("Text_1").value);
params.append('command[]', document.getElementById("Text_2").value);
xmlhttp.send(params);
现在 $_POST['command']
将是一个数组,因此您必须 运行 命令如下:
<?php
$parameters = array_map('escapeshellarg', $_POST['command']);
$output = exec("someexecutable.exe ".implode(' ', $parameters));
?>