【问题标题】:How to filter user' s input ( html, with PHP backend) with python?如何使用 python 过滤用户的输入(html,带有 PHP 后端)?
【发布时间】:2014-09-28 12:41:30
【问题描述】:

有一个用 PHP 和 HTML 编写的 Web 应用程序。我想要的是针对各种情况过滤用户输入并对其进行清理。例如,我想将来自表单(字符串)的输入与允许的字符串列表进行比较,并根据触发合适的 PHP 函数来处理它是对还是错。

我的问题是如何将用户输入与 python 脚本绑定,然后将此 python 脚本的结果作为 PHP 的输入?

谢谢

【问题讨论】:

    标签: php python html user-input input-sanitization


    【解决方案1】:

    您可以将 PHP 文件中的 Python 脚本作为 shell 命令调用,并传递 JSON 格式的参数。然后让 Python 脚本输出响应(也是 JSON 编码的)并让 PHP 文件捕获它。这是我最近使用的一个示例,由以下链接拼凑而成:

    PHP 文件:

    $py_input = ... // Your data goes here.
    // Call the Python script, passing it the JSON argument, and capturing the result.
    $py_output = shell_exec('python script.py ' . escapeshellarg(json_encode($py_input)));
    $py_result = json_decode($py_output);
    

    Python 文件:

    import json
    
    php_input = json.loads(sys.argv[1])  # The first command line argument.
    # Do your thing.
    php_output = ... # Whatever your output is.
    print json.dumps(php_output) # Print it out in JSON format.
    

    Passing a Python list to php

    executing Python script in PHP and exchanging data between the two

    【讨论】:

      猜你喜欢
      • 2013-01-14
      • 2010-11-05
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多