【问题标题】:Send string to php server and use it将字符串发送到 php 服务器并使用它
【发布时间】:2015-09-25 02:35:49
【问题描述】:

我正在尝试将字符串发送到 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 中字符串的值。

【问题讨论】:

  • 命令发送前的值是多少?那么 $_POST 数组的值是多少呢?
  • 可能想在您的$parameter 上使用escapeshellarg。如果我可以在你的服务器上运行任意命令,我可以做一些非常讨厌的事情(比 SQL 注入更糟糕)!
  • 到目前为止command的值是text_1和text_2的串联,所以我确实确认了我发送的字符串是好的。对于 $_POST,我不知道,因为它的服务器端我不知道如何检查它,因为我不能使用警报框之类的东西来弹出内容。我试图简单地在服务器上捕获它并将其扔回以在 javascript 客户端读取它,但它不起作用。它把我扔回垃圾。但我得到了这个错误:未定义的错误索引:命令。
  • @Rocket Hazmat 你到底是如何使用它的,它又是如何保护我的? (我在 1 周半前开始使用 js/css/php...)
  • @MacGruber:escapeshellarg($_POST['command'])。它保护字符串,因此它被视为一个参数。如果我向您的服务器发送以下命令会发生什么:exit &amp;&amp; del file 或类似的东西?您的服务器将运行someexecutable.exe exit &amp;&amp; del fileescapeshellarg 会变成 someexecutable.exe 'exit &amp;&amp; del file'。 (编辑:我是 Linux 人,必须为 Windows 更新命令。)

标签: php xmlhttprequest


【解决方案1】:

setRequestHeader 用于设置请求的标头。 Content-typeContent-length 之类的东西。

您需要将数据传递给send()。要使$_POST 工作,它们需要采用key=val&amp;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);

附:您应该在运行命令之前运行escapeshellarg()。如果人们可以在您的服务器上运行任意命令,这可能比 SQL 注入更糟糕。

<?php
$parameter = escapeshellarg($_POST['command']);
$output = exec("someexecutable.exe $parameter");
?>

附言escapeshellarg() 将使您的命令将 整个 $_POST['command'] 字符串视为 one 参数。如果你不想这样,那么你需要从你的 JavaScript 中发布一个 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));
?>

【讨论】:

  • 天哪,它起作用了,我感谢你@Rocket Hazmat!
猜你喜欢
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 2014-07-19
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多