【问题标题】:What exactly does the PHP function "test_input()" do?PHP 函数“test_input()”究竟做了什么?
【发布时间】:2015-01-26 19:17:09
【问题描述】:

我知道我会因此而被否决,但我需要问一下。下面的代码是什么意思?具体来说, 我很困惑

if (empty($_POST["comment"])) { $comment = ""; 

这是否意味着如果用户没有插入他的 cmets,那么值为 "$comment" 的字段将是空白的?如果用户插入了评论,那么值“$comment”将通过 test_input 函数输入?我看过 w3schools 网站,但我并不聪明。令人难以置信的是,没有网站甚至描述了 test_input 所做的事情,相信我,我看过几十个网站。事实上,我并不是唯一一个认为 test_input 是一个神秘函数的 SO 用户。

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    echo 'posted';
    if (empty($_POST["comment"])) {
        $comment = "";
    } else {
        $comment = test_input($_POST["comment"]);
    }

【问题讨论】:

  • 是的,就是这个意思。
  • "如果发布的comment 字段是empty,则将$comment 设置为空字符串。但是,如果它不为空,则将$comment 设置为结果以注释为参数的test_input 函数" test_input 很可能是您的 PHP 代码中使用function test_input 定义的某个函数。
  • 这个问题似乎跑题了,因为“test_input()”不是一个php函数。

标签: php sql


【解决方案1】:

test_input 是用户定义的函数,而不是 PHP 内置的函数。查看代码确实意味着如果没有插入注释,那么它将是空的,但是如果它不为空,则通过 test_input 函数运行注释。

在这个页面http://www.w3schools.com/php/php_form_validation.asp我们可以看到他们在哪里声明了test_input函数,特别是页面的这一部分

<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = test_input($_POST["name"]);
  $email = test_input($_POST["email"]);
  $website = test_input($_POST["website"]);
  $comment = test_input($_POST["comment"]);
  $gender = test_input($_POST["gender"]);
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

例如,假设您希望在每条评论的末尾加上日期和时间戳,下面的代码将实现这一点。

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
     echo 'posted';

     if (empty($_POST["comment"]))
     {
         $comment = "";
     }
     else
     {
        $comment = add_date($_POST["comment"]);
     }
 }

 function add_date($comment)
 {
      return $comment . date('d-m-Y G:i:s');
 }

因此,如果用户输入 Hello this is a comment,$comment 现在将是 Hello this is a comment 28-11-2014 16:00:00

所以最后确定,test_input 只是 w3schools 创建的一个函数名称,只要页面引用任何类型的输入验证等,他们就会使用它。

【讨论】:

    【解决方案2】:

    test_input()是w3schools使用的用户自定义函数:

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    

    例如:

    <?php
      // define variables and set to empty values
      $name = $email = $gender = $comment = $website = "";
    
      if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = test_input($_POST["name"]);
        $email = test_input($_POST["email"]);
        $website = test_input($_POST["website"]);
        $comment = test_input($_POST["comment"]);
        $gender = test_input($_POST["gender"]);
      }
    
      function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
      }
    ?>
    
    • trim($data) 将从用户输入数据中去除不必要的字符(多余的空格、制表符、换行符)(使用 PHP trim() 函数)。
    • stripslashes($data) 将从用户输入数据中删除反斜杠 ()(使用 PHP stripslashes() 函数)。
    • htmlspecialchars($data) 将特殊字符转换为 HTML 实体。
      • (如果用户输入&amp;lt; 和&amp;gt;,htmlspecialchars() 会将其转换为&amp;lt; 和&amp;gt;)。

    【讨论】:

      【解决方案3】:

      没有像 test_input() 这样的 PHP 函数。

      它可能是 w3schools 网站中的用户定义函数。只需访问您提到的网站并运行示例代码,然后在 tryit 编辑器中检查一次源代码,您就可以在那里获得该功能。

      有时网站在解释的时候没有显示所有功能,所以请参考源代码

      【讨论】:

      • 上述函数定义为here。
      【解决方案4】:

      test_input() 不是标准的 php 函数,但你知道你可以像这样创建自己的函数:

      function test_input($input){
         $output = $input." Oh, I'm updated."; // Whatever process you want to make
         return $output;
      }
      

      然后你可以在你的php脚本中调用test_input()。

      所以基本上你应该问作者这是做什么的,或者只是搜索test_input()的定义。

      如果您使用框架、库或插件,则定义了大量非标准函数,您可以查看文档(api)或阅读源代码以找出答案。

      也许你可以发布整个脚本,也许我们可以找出test_input()在哪里。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-23
        • 2016-09-10
        • 2023-03-15
        • 2012-10-17
        • 2021-06-04
        • 1970-01-01
        • 2018-07-30
        • 2019-10-06
        相关资源
        最近更新 更多