简体   繁体   English

PHP用户输入数据过滤以提交到MySQL数据库-建议

[英]PHP user input data filtering for submission to MySQL database - recommendations

I'm building a class for filtering data, and I've been compiling various recommendations. 我正在建立一个用于过滤数据的类,并且一直在编译各种建议。

This is primarily to avoid faulty data from user input in the database, and also an additional level to help prevent not yet thought of types of injection attacks, etc. (NOTE: this does NOT replace the need to also use prepared statements with any data submitted to a database.) 这主要是为了避免来自数据库中用户输入的错误数据,并且还可以提供一个附加级别,以帮助防止尚未想到的注入攻击类型等。 (注意:这并不能代替将准备好的语句也与任何数据一起使用的需求。提交到数据库。)

As much as possible, I do not want to return an error, I want to "make the data work". 尽可能,我不想返回错误,我想“使数据正常工作”。 It's assuming someone accidentally typed a ; 假设有人不小心输入了; or ' etc. in an input field, where it can't be accepted. 或'等。在无法接受的输入字段中。 Or left in thousand separators (,) in a number where they shouldn't be. 或以不应该使用的数字形式留在千位分隔符(,)中。 So just take it out and continue. 因此,只需将其取出并继续。

I wanted to put this out there for others to critique and use. 我想把它放在那里供其他人批评和使用。 I know there are other questions about this type of thing, but I haven't seen any with a combined recommendation for various types. 我知道关于这种类型的事情还有其他问题,但是我还没有看到针对各种类型的组合建议的任何问题。

My question is - what would you do differently? 我的问题是-您会怎么做? Would you be concerned about users entering a number like "47387.284.02"? 您是否会担心用户输入的数字为“ 47387.284.02”? If so, how could I eliminate the the second dot (decimal point, period) and everything after? 如果是这样,我如何消除第二个点(小数点,句点)以及之后的所有内容? (While still allowing numbers like ".75" and "10.20") (尽管仍允许使用“ .75”和“ 10.20”之类的数字)

// Use for numbers - integers, floats
function filterNumbers($data) {
    $data = trim(htmlentities(strip_tags($data)));
    $data = preg_replace('/[^.0-9]/', "", $data); // only numeric values allowed (and decimal point)
    $data = filter_var($data, FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    $data = mysqli_real_escape_string($GLOBALS['con2'], $data);
    return $data;
}

// Use for short strings - alphanumeric only - usernames, varieties, etc.
function filterExtreme($data) {
    $data = trim(htmlentities(strip_tags($data)));
    $data = preg_replace('/[^ ._A-Za-z0-9]/', "", $data);
    $data = mysqli_real_escape_string($GLOBALS['con'], $data);
    return $data;
}

// Use for email addresses
function filterEmail($data) {
    $data = filter_var($data, FILTER_SANITIZE_EMAIL);
    $data = mysqli_real_escape_string($GLOBALS['con'], $data);
    return $data;
}

// Use for comments where some special characters may be desired
function filterComment($data) {
    $data = trim(htmlentities(strip_tags($data)));
    $data = filter_var($data, FILTER_SANITIZE_STRING,FILTER_FLAG_ENCODE_HIGH);
    $data = mysqli_real_escape_string($GLOBALS['con'], $data);
    return $data;
}

Note: $con is the connection details to the MySQL database. 注意:$ con是与MySQL数据库的连接详细信息。

You are correct to be concerned with proper data entry, but I agree with Bill Karwin's comments above (I can't make a comment due to my rep). 您正确的数据输入是正确的,但是我同意Bill Karwin的上述评论(由于我的代表,我无法发表评论)。 There's no way to know the user's intent, so "making it work" might make things worse for them in the end. 无法知道用户的意图,因此“使它起作用”最终可能会使他们感到更糟。 If they entered a ';' 如果他们输入“;” then it's possible they got something else wrong too. 那么很可能他们也会出错。

Preventing unwanted character entry should be handled via the interface as much as possible, and your back-end code should simply test for compliance. 防止不必要的字符输入应尽可能通过接口处理,而后端代码应仅测试一致性。 If it fails, return an error code and let the front-end deal with it. 如果失败,则返回错误代码,并让前端对其进行处理。

Stripping commas from numbers or trimming white-space probably isn't too bad, but that's also something I'd try to handle on the front whenever possible. 从数字中删除逗号或修剪空格可能还不错,但这也是我尽可能尝试的方法。 You just have to deal with it as needed. 您只需要根据需要进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM