PHP input sanitizer?

后端 未结 4 923
余生分开走
余生分开走 2021-01-06 07:47

What are some good PHP html (input) sanitizers?

Preferably, if something is built in - I\'d like to us that.

UPDATE:

Per the request

相关标签:
4条回答
  • 2021-01-06 08:00

    I've always used PHP's addslashes() and stripslashes() functions, but I also just saw the built-in filter_var() function (link). Looks like there are quite a few built-in filters.

    0 讨论(0)
  • 2021-01-06 08:11

    If you want to run a query that use let's say $_GET['user'] a nice solution would be to do something like this using mysql_real_escape_string():

    <?php
    
        $user = mysql_real_escape_string($_GET['user']);
        $SQL = "SELECT * FROM users WHERE username = '$name'";
    
        //run $SQL now
        ...
    ?>
    

    If you want to store a text in a database and then print it on a web page, consider use htmlentities

    [Edit]Or as awshepard said, you can use addslashes() and stripslashes() functions[/Edit]

    Here is a little example of sanitization when it comes to prevent XSS attacks:

    <?php
        $str = "A 'quote' is <b>bold</b>";
    
        //Outputs: A 'quote' is <b>bold</b>
        echo $str;
    
        // Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
        echo htmlentities($str);
    
        // Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;
        echo htmlentities($str, ENT_QUOTES);
    ?>
    
    0 讨论(0)
  • 2021-01-06 08:14

    use

     $input_var=sanitize_input($_POST);
    

    and functions are below, almost sanitize everthing u need

    function sanitize($var, $santype = 1){
         if ($santype == 1) {return strip_tags($var);}
         if ($santype == 2) {return htmlentities(strip_tags($var),ENT_QUOTES,'UTF-8');}
         if ($santype == 3) 
         {
          if (!get_magic_quotes_gpc()) {
           return addslashes(htmlentities(strip_tags($var),ENT_QUOTES,'UTF-8'));
          } 
          else {
             return htmlentities(strip_tags($var),ENT_QUOTES,'UTF-8');
          }
         }
        }
    

        function sanitize_input($input,$escape_mysql=false,$sanitize_html=true,
                 $sanitize_special_chars=true,$allowable_tags='<br><b><strong><p>')
        {
          unset($input['submit']); //we use 'submit' variable for all of our form
    
          $input_array = $input;
    
          //array is not referenced when passed into foreach
          //this is why we create another exact array
          foreach ($input as $key=>$value)
          {
           if(!empty($value))
           {
            $input_array[$key]=strtolower($input_array[$key]);
            //stripslashes added by magic quotes
            if(get_magic_quotes_gpc()){$input_array[$key]=sanitize($input_array[$key]);} 
    
            if($sanitize_html){$input_array[$key] = strip_tags($input_array[$key],$allowable_tags);}
    
            if($sanitize_special_chars){$input_array[$key] = htmlspecialchars($input_array[$key]);}    
    
            if($escape_mysql){$input_array[$key] = mysql_real_escape_string($input_array[$key]);}
           }
          }
    
          return $input_array;
    
        }
    

    Remember : it will not sanitize multidimensional array, u need to modify it recursively.

    0 讨论(0)
  • 2021-01-06 08:22

    html purifier -> http://htmlpurifier.org/

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