0

重複の可能性:
SQL インジェクションを防ぐ最善の方法は?

私は、JavaScript と入力ファイルの種類を使用してファイル名を取得するためだけに、ユーザーがファイルの場所を示すことしかできず、アップロードすることを想定していないファイル システムを作成しています。

問題は、挿入するクエリを実行するときです...ファイルのパスは次のとおりです。

C:\users\files\test.php

ここでの問題は、PHP で次の文字が省略される \ 文字です。したがって、データベースに C:usersfilestest.php として保存されます。

ユーザーがファイルの場所を指定する別の入力ボックスがあります...だから、彼らは次のように書きます:

C:\ \ 文字のため、クエリは実行されません。

何かご意見は?

4

2 に答える 2

1

If you properly escape the string you are trying to insert, you will not have the problem with the \ disappearing. It is actually MySQL that is treating the single backslash as an escape string.

So something like this:

$path_string_escaped = mysqli_real_escape_string($db_connection, $path_string);

And use $path_string_escaped in your insert.

Note that this with make $path_string_escaped look something like C:\\users\\files\\test.php. MySQL will then unescape the backslashes such that C:\users\files\test.php will be written to database. You will not need to do anything when reading from the database to modify the string.

You should ALWAYS escape user-provided or user-accessible data you will be writing to the database.

This is also a good example of why, even if using prepared statements, that you should still escape user-accessible data you are writing to DB when you are dealings with string that could contain MySQL escape sequences. While using prepared statements can certainly prevent against an actual injection attack, it will not help you actually write your data to the database the way you intend when there are already MySQL escape sequences in the strings.

于 2012-11-09T16:18:14.890 に答える
1

\\シングルでも使える\

于 2012-11-09T16:03:47.767 に答える