繁体   English   中英

如何使用符号(+,*,/, - )发送包含php源的帖子数据,并使用ajax请求将该数据写入文件?

[英]How to send post data which contains php source with symbols like (+, *, /, -) and writing that data to a file using ajax request?

所以完整的场景是,我正在制作一个看起来像w3schools的网络应用程序“自己试试吧!” 选项。 但截至目前,关于安全性并使其成为Web应用程序的核心部分。

我的逻辑是:

  1. 使用textarea从用户获取输入并使用POST方法在ajax请求中发送该数据。
  2. 现在在一个PHP文件中(ajax向其发送请求),我将发布的内容写入另一个PHP文件并使用系统函数执行该PHP代码并再次将生成的html写入.htm文件。
  3. 现在在ajax请求发送的文件(第一个文件)中,我在javascript中使用.htm文件输出的iframe并刷新它。

问题是:

使用包含echo,系统功能的代码,一切正常。 但是当我们使用任何符号如'+'时,发布的数据将转换为html url解码。

<?php
  echo "Hello, world";
?>

上面的代码工作正常。

<?php
  $a = 10;
  $b = 20;
  echo $a+$b;
?>

它不起作用。 上面的代码是在PHP编写的文件中检查的(我从POST数据写到),它写成:

<?php
  $a = 10;
  $b = 20;
  echo $a $b;
?>

'+'符号正在转换为空格。 我猜数据被解码成普通的html实体。 这整个事情是否有更好的逻辑,以及如何解决这个问题。 请帮帮我们。

使用ajax请求发送textarea代码的文件:

<title>Html Black Box</title>
<style>
    textarea{ 
    font-family: courier; font-size: 15px; height: 100%; width: 100%;}
    input {width: 100%; height: 100%; font-size: 40px;}
    iframe {width: 100%; height: 100%; }
</style>
<script>
function update(){
document.getElementById('tdop').innerHTML = "<img src=ajx_ima.gif>";
var form = document.getElementById('code_text').value;
var xmlhttp;
    if (window.XMLHttpRequest)
      xmlhttp=new XMLHttpRequest();
    else
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

xmlhttp.open("POST", "process_php.php", true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.onreadystatechange=function()
  {
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        var res = xmlhttp.responseText;
    document.getElementById('tdop').innerHTML = "<iframe src=out_php.htm id=out></iframe>";
    }

  }
    xmlhttp.send("code="+form);
}
</script>
<table border=0>
<tr>
<td>
    <textarea id="code_text" style="resize: none;"></textarea>
</td>
<td id="tdop">
    <iframe src="blank.htm" id="out" frameBorder="1"></iframe>
</td>
</tr>
<tr><td colspan="2" align="center"><input type="button" value="Update Page" onclick="update()"></td></tr>
</table>

写入接收数据的文件

<?php
$stringData = ''.isset($_POST['code']) ? $_POST['code'] : '';
$myFile = "buf_php.php";
$fh = fopen($myFile, 'w+');
file_put_contents($myFile, $stringData);
fclose($fh);
$myFile = "out_php.htm";
system("php buf_php.php > $myFile");
echo $stringData;
?>

您是通过邮寄请求直接从您的textarea发送“代码”。 特殊字符将被处理,因为它是一个网址。

要绕过这个,你必须使用encodeURIComponent()函数在javascript中对这些特殊字符进行编码,该函数escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( ) escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )

....
 // var form = document.getElementById('code_text').value; //old line
 var form = encodeURIComponent(document.getElementById('code_text').value)
....

除了+ -sign之外,这将解决所有问题。 所以你必须用“%20”取代+

...
 var form = encodeURIComponent(document.getElementById('code_text').value);
 form = form.replace('+','%20');
....

你说你想要替换*。 以前的URL为您提供了一个“固定”功能,可以替换为:

function fixedEncodeURIComponent (str) {
    return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM