The server accepts data in which you can easily write any script or try to break through the protection on the server or database with quotes. I try to close all vulnerabilities as clean as possible, but there are two conditions:
- Quotes and line breaks should be left. In this case, line breaks come from textarea, so that there will be a replacement
\non<br>. - When checking for empty field content - the field is only with characters, that is, quotation marks or dots should not pass the test and the answer will be "empty field".
At the beginning of the script, I have a function installed that clears the $ _POST array from too much, it looks like this:
function quote_clear($data) { $data = str_replace("\n", '<br>', $data); $data = strip_tags($data, '<br>'); $data = htmlspecialchars($data, ENT_QUOTES); return $data; } function quote($var) { foreach ($var as $row) { if(is_array($row)) { foreach($row as $row2) { $row2 = quote_clear($row2); } } else { $row = quote_clear($row); } } return $var; } But in the comments they said that the real data is usually transferred to the database, with all tags, quotes and anything else. I see a kind of benefit from this now, but I do not understand how protection works in this case.
That is, we remove my sweep function, and now anything can get into the database, and SQL injections are just waiting for the world to be captured. I have already read through the topics with ingenious tips "each variable has its own protection", and no example in addition.
The site is a form in which there is:
- input (text) - for E-mail, mobile phone, customer name, links, and so on.
- input (checkbox) - obviously just a choice of categories that the customer wants to include in the request.
- textarea - comments for each category selected in the checkbox.
I believe that any of these inputs can be corrected on the client, which means that all should be given certain security measures. Initially, I thought that if you remove all the special characters, tags, and to film all that remains, it will be as safe as possible, and most importantly universally, but now I don’t understand what is safety at all.
Could you give me an example of data protection in my case? I am using mysqli.
Thank you for attention.
htmlspecialchars. - yeputonshtmlspecialchars_decode) the html code that remained and that we did not lose anything important. (sort of like) - Telion<script>alert(1);</script>"; drop table questions; --- then it should be stored in the database and nothing should be cut out. As you can see, the stackoverflow saved my comment and did not cut anything in it, but at the same time, no code written by me failed :) To this and need to strive. - andreymal