The ultimate clean/secure function

后端 未结 7 2251
忘掉有多难
忘掉有多难 2020-11-22 03:11

I have a lot of user inputs from $_GET and $_POST... At the moment I always write mysql_real_escape_string($_GET[\'var\'])..

I

相关标签:
7条回答
  • 2020-11-22 03:31

    i used that pass array or get , post

    function cleanme(&$array)
    { 
     if (isset($array))
     {
         foreach ($array as $key => $value)
         {
              if (is_array($array[$key]))
              {
               secure_array($array[$key]);
              }
              else 
              {
                $array[$key] = strip_tags(mysql_real_escape_string(trim($array[$key])));
              }
         }
     }
    }
    

    Usage :

    cleanme($_GET);   
    cleanme($_POST);
    
    0 讨论(0)
  • 2020-11-22 03:42

    The idea of a generic sanitation function is a broken concept.

    There is one right sanitation method for every purpose. Running them all indiscriminately on a string will often break it - escaping a piece of HTML code for a SQL query will break it for use in a web page, and vice versa. Sanitation should be applied right before using the data:

    • before running a database query. The right sanitation method depends on the library you use; they are listed in How can I prevent SQL injection in PHP?

    • htmlspecialchars() for safe HTML output

    • preg_quote() for use in a regular expression

    • escapeshellarg() / escapeshellcmd() for use in an external command

    • etc. etc.

    Using a "one size fits all" sanitation function is like using five kinds of highly toxic insecticide on a plant that can by definition only contain one kind of bug - only to find out that your plants are infested by a sixth kind, on which none of the insecticides work.

    Always use that one right method, ideally straight before passing the data to the function. Never mix methods unless you need to.

    0 讨论(0)
  • 2020-11-22 03:45

    May I suggest to install "mod_security" if you're using apache and have full access to server?!
    It did solve most of my problems. However don't rely in just one or two solutions, always write secure code ;)
    UPDATE Found this PHP IDS (http://php-ids.org/); seems nice :)

    0 讨论(0)
  • 2020-11-22 03:46

    There is no point in simply passing the input through all these functions. All these functions have different meanings. Data doesn't get "cleaner" by calling more escape-functions.

    If you want to store user input in MySQL you need to use only mysql_real_escape_string. It is then fully escaped to store safely in the database.

    EDIT

    Also note the problems that arise with using the other functions. If the client sends for instance a username to the server, and the username contains an ampersand (&), you don;t want to have called htmlentities before storing it in the database because then the username in the database will contain &.

    0 讨论(0)
  • 2020-11-22 03:47

    You're looking for filter_input_array(). However, I suggest only using that for business-style validation/sanitisation and not SQL input filtering.

    For protection against SQL injection, use parametrised queries with mysqli or PDO.

    0 讨论(0)
  • 2020-11-22 03:49

    The problem is, something clean or secure for one use, won't be for another : cleaning for part of a path, for part of a mysql query, for html output (as html, or in javascript or in an input's value), for xml may require different things which contradicts.

    But, some global things can be done. Try to use filter_input to get your user's input. And use prepared statements for your SQL queries.

    Although, instead of a do-it-all function, you can create some class which manages your inputs. Something like that :

    class inputManager{
      static function toHTML($field){
        $data = filter_input(INPUT_GET, $field, FILTER_SANITIZE_SPECIAL_CHARS);
        return $data;
      }
      static function toSQL($field, $dbType = 'mysql'){
        $data = filter_input(INPUT_GET, $field);
        if($dbType == 'mysql'){
          return mysql_real_escape_string($data);
        }
      }
    }
    

    With this kind of things, if you see any $_POST, $GET, $_REQUEST or $_COOKIE in your code, you know you have to change it. And if one day you have to change how you filter your inputs, just change the class you've made.

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