SQL injection and sound acceleration are magical for many people, a kind of protection against some mysterious danger, but: do not be afraid of it - this is not magic. This is just a way to include special characters processed by the request.
So, do not invent new magic shields and ways to protect the magical danger of injections! . Instead, try to understand how input escaping works.
It would be best to just see what really happens. Let's say the input line is:
');DROP TABLE table;
after release:
\');DROP TABLE table;
in fact, he escaped a single slash. This is the only thing you need to make sure that when you insert a line into the query, the syntax will be OK!
insert into posts set title = '\');DROP TABLE table;--'
This is not magic, like a firewall or something like that, just make sure the resulting query has the correct syntax! (of course, if it is not, it can be used)
The query analyzer then looks at the sequence and knows that it is still a variable, not its value. It will remove the backslash and the following data will be stored in the database:
');DROP TABLE table;
which is exactly the same value as entered by the user. And this is exactly what you wanted in the database!
So this means that if you select this row from the database and want to use it again in the query, you need to avoid it again to make sure that the resulting query has the correct syntax .
But in your example it is very important to note the magic_quotes_gpc directive!
This function automatically skips all user input (gpc - _GET, _POST and _COOKIE). This is an evil feature made for people not familiar with SQL injection. This is evil for two reasons. . The first reason is that then you need to distinguish between the case of the first and second requests - in the first case you do not run away, and in the second you do. What most people do is either turn off the "function" (I prefer this solution), or turn off user input first, and then, if necessary, escape from it. The unescape code might look like this:
function stripslashes_deep($value) { return is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); } if (get_magic_quotes_gpc()) { $_POST = stripslashes_deep($_POST); $_GET = stripslashes_deep($_GET); $_COOKIE = stripslashes_deep($_COOKIE); }
The second reason this evil is because
there is nothing to do with "universal quoting .
" When quoting, you
always quote text for a specific output file , for example:
- string value for mysql query
like expression for mysql query- html code
- Json
- mysql regex
- php regex
For each case, you need a different quote, because each use is present in a different syntax context. This also implies that quoting should not be done at the input to PHP, but at a specific output ! For this reason, functions like magic_quotes_gpc are broken ( never forget to handle it, or better, make sure it is turned off !!! ).
So, what methods can be used for citation in these specific cases? (Feel free to correct me, there may be more modern methods, but they work for me)
mysql_real_escape_string($str)mysql_real_escape_string(addcslashes($str, "%_"))htmlspecialchars($str)json_encode() - only for utf8! I use my function for iso-8859-2mysql_real_escape_string(addcslashes($str, '^.[]$()|*+?{}')) - you cannot use preg_quote in this case, because the backslash will be reset twice!preg_quote()