考虑以下代码:PHP - 当过滤用户输入
// store the $name into a database
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
// already encrypted password (client-side) and will be salted or hashed again to store it into DB
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
// just to check if the $lang is on the $language array i.e. in_array($lang, $language)
$lang = filter_input(INPUT_POST, 'language', FILTER_SANITIZE_STRING);
// just to echo something
$sth = filter_input(INPUT_POST, 'sth', FILTER_SANITIZE_STRING);
在这里,我试图描述的一些情况。并且在每种情况下,我筛选了用户输入(在某些情况下不需要)。我的问题是何时过滤用户输入,因为它似乎过滤用户输入并不总是必要的。
您应该*总是*过滤用户输入。永远不要相信他们的输入。请使用PHP的[内置函数](http://jayblanchard.net/proper_password_hashing_with_PHP.html)来处理密码安全性。如果您使用的PHP版本低于5.5,则可以使用'password_hash()'[兼容包](https://github.com/ircmaxell/password_compat)。 –
对于SQL查询,您应该了解[PDO](http://php.net/manual/en/pdo.prepared-statements)的[prepared](http://en.wikipedia.org/wiki/Prepared_statement)语句。 php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php),并考虑使用PDO,[这真的很简单](http://jayblanchard.net/demystifying_php_pdo的.html)。 –
它不是正确的问题要求在stackoverflow,请删除它 –