如何更改用户的html表单输入并在evrey的

2024-04-26 09:48:45 发布

您现在位置:Python中文网/ 问答频道 /正文

这似乎很复杂。你知道吗

我有一个html表单,允许用户上传python代码。提交时,python代码位于全新页面的文本区域中,例如:

<textarea id='yourcode' cols='40' rows='10'>
<?php echo 'inch = 1 
centimeter = 2.54 
inch1 = float(raw_input('Enter the value in inches 
that you want to convert. ')) 
print "A value of %r inches is equal to %r centimeters." 
% (inch1,inch1 * inch * centimeter) '; ?>
</textarea>

如您所见,python“与php交互”并导致错误。是否仍然可以自动通读insert并添加\以便python中的“”不会交互。它会将上面的代码(不起作用)更改为

<textarea id='yourcode' cols='40' rows='10'>
<?php echo 'inch = 1 
centimeter = 2.54 
inch1 = float(raw_input(\'Enter the value in inches 
that you want to convert. \')) 
print "A value of %r inches is equal to %r centimeters." 
% (inch1,inch1 * inch * centimeter) '; ?>
</textarea>

上面的这些会有用的。抱歉,如果这是完整的,并问你是否有问题。有没有人知道如何创建这个脚本,自动在用户输入的python代码前面添加一个“\n”来破坏脚本。谢谢


Tags: to代码用户echoidvaluerowsphp
2条回答

如果在PHP代码中嵌入了Python代码,请在PHP中使用heredoc

<textarea id='yourcode' cols='40' rows='10'>
<?php $text = <<<EOT
    inch = 1 
    centimeter = 2.54 
    inch1 = float(raw_input('Enter the value in inches 
      that you want to convert. ')) 
    print "A value of %r inches is equal to %r centimeters." 
      % (inch1,inch1 * inch * centimeter)
EOT; 
echo $text; ?>
</textarea>

更简单,没有中间变量:

<textarea id='yourcode' cols='40' rows='10'>
<?php echo <<<EOT
    inch = 1
    centimeter = 2.54
    inch1 = float(raw_input('Enter the value in inches
      that you want to convert. '))
    print "A value of %r inches is equal to %r centimeters."
      % (inch1,inch1 * inch * centimeter)
EOT; ?>
</textarea>

使用filter_var()防止有害HTML代码的更安全版本:

<textarea id='yourcode' cols='40' rows='10'>
<?php echo filter_var( <<<EOT
    inch = 1
    centimeter = 2.54
    inch1 = float(raw_input('Enter the value in inches
      that you want to convert. '))
    print "A value of %r inches is equal to %r centimeters."
      % (inch1,inch1 * inch * centimeter)
EOT
    , FILTER_SANITIZE_FULL_SPECIAL_CHARS ?>
</textarea>

遗传病不必逃脱。但是,存在一些安全问题。但真正的问题是:如何从表单中获取用户输入字符串?你知道吗

addslashes($thePythonTextYouWantToAdd\)

相关问题 更多 >