What exactly does the PHP function “test_input()” do?

前端 未结 4 627
夕颜
夕颜 2021-02-04 11:01

I know I will be voted down for this, but I need to ask. What does the code below mean? Specifically, I am confused by

if (empty($_POST[\"comment\"])) { $comme         


        
相关标签:
4条回答
  • 2021-02-04 11:21

    There is no any such PHP function like test_input().

    It might be the user defined function in w3schools website. Just go on the website you mentioned and run the example code and in tryit editor check the source code once, you may get that function there.

    Some times websites dont display all the functions while giving the explanation, so just refer the source code

    0 讨论(0)
  • 2021-02-04 11:26

    test_input is a user defined function and not a function built into PHP. Looking at the code it does indeed mean that if no comment has been inserted then it will be empty, however if it is not empty run the comment through the test_input function.

    On this page http://www.w3schools.com/php/php_form_validation.asp we can see where they have declared the test_input function, specifically this part of the page

    <?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;
    }
    ?>
    

    Lets say for example that every comment that was made you wanted to put a date and timestamp on the end, the following code would achieve this.

    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');
     }
    

    Therefore if the user entered Hello this is a comment, $comment would now be Hello this is a comment 28-11-2014 16:00:00

    So to finalize, test_input is simply a function name created by w3schools and they use it whenever the page refers to any kind of validation of input etc.

    0 讨论(0)
  • 2021-02-04 11:29

    test_input() is a user - defined function used by w3schools:

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

    For example:

    <?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) will strip unnecessary characters (extra space, tab, newline) from the user input data (with the PHP trim() function).
    • stripslashes($data) will remove backslashes () from the user input data (with the PHP stripslashes() function).
    • htmlspecialchars($data) converts special characters to HTML entities.
      • (If the user inputs < and >, htmlspecialchars() will translate it to &lt; and &gt;).
    0 讨论(0)
  • 2021-02-04 11:38

    test_input() is not a standard php function but you know you can make your own functions just like this:

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

    Then you can call test_input() in your php script.

    So basically you should ask the author what this does or just search for definition of test_input().

    If you use frameworks, libraries or plugins, there are tons of non-standard functions defined and what you can is to check the document(api) or to read the source code to find out the answer.

    Perhaps you can post the whole script, maybe we can find out where test_input() is.

    0 讨论(0)
提交回复
热议问题