For a long time I have been tormented by the question: How to protect your site from SQL-Injection. In the old days, I used mysql_real string_escape to escape quotes. But some time has passed, and I learned about PDO , they say, they say, this topic will help well in protecting the site. Say, I am not a great hacker and I can’t verify it in any way, I know that with mysql_real string_escape all quotes and other nonsense were screened - and voila, you are already in the house.
Now I am working on my project and decided to check the site for penetration protection. As a result, what I did:
I just took and entered data with quotes, for example, Infor`mat'ion. As I have already said, I am not a great hacker, but I know for sure that all this rubbish should be screened, but it was so entered into the database, which prompted me to think that I don’t have any protection at all. And I entered the data like this:
$sql = "INSERT INTO chat (time, login, private, text) values(?, ?, ?, ?)"; $db->prepare($sql)->execute(array($time, $login, $private, $text));` I read this method on a habre in "Why you should use PDO to work with a database," the connection is the same as indicated in the article.
How can you protect against these injections?
но точно знаю, что вся эта дрянь должна экранироваться, но она так и заносилась в базу данных. This rubbish was screened out, so that it would be recorded in the database in the form that is and did not affect the execution of the query, including, did not change its behavior, thereby squeezing SQL injections. Those. Shielding did its job, exactly what the user entered into the database. Now, if your query was broken off, it would be very bad, because it allowed inserting into the text a piece of SQL to execute it. - Vitalts