Answer the question
In order to leave comments, you need to log in
Precisely and in detail about injection protection in php, mysql?
Hello! I rummaged through a bunch of different information about protecting user-entered data, but I did not find an exact explanation of how to properly protect myself. On forums, guides and other sources of information, everything always comes down to one thing - a person writes a guide on how to "correctly" defend himself, then in the comments people write that he is a fool and only teaches people bad things. As a result, no one left normal and accurate information.
Let's take as an example such a task, the most elementary and which is constantly talked about - password protection, login on the authorization form. Someone writes that you need to do this:
$login = trim($_GET['login']);
$login = strip_tags($login);
$login = mysqli_real_escape_string($mysqli, $login);
$login = $_GET['login'];
$login = mysqli_real_escape_string($mysqli, $login);
Answer the question
In order to leave comments, you need to log in
$login = trim($_GET['login']);
$login = strip_tags($login);
$login = mysqli_real_escape_string($mysqli, $login);
Why do you think that you need to change the user's login if she wanted just that? wants to be<script>
- let it be. Don't limit.
you need to distinguish between sql injection and xss attack
i.e. if we write to the database, then
$login = mysqli_real_escape_string($mysqli, $login);
query( insert into ... );
Next, we display on the page
$login = query( select from ... );
echo htmlspecialchars($login);
And everyone is happy.
Too lazy to write the code, although I basically already wrote it above =) but instead of mysqli_real_escape_string in real code, I would use prepared expressions (and PDO instead of mysqli, although this is probably a matter of taste).
Read the article habrahabr.ru/post/148701
It describes in detail why what you are doing (these two pieces of disgusting code that you have given) is unacceptable and leads to SQL injections.
Validation of input variables is done in 2 stages:
1. validfilter() - Check input variables from users for validity using Regex.
2. mysql_escape_mimic() - Escaping before placing a query to the database in the expression.
(working code!)
//----------------------
function mysql_escape_mimic($inp) {
//from: http://php.net/manual/en/function.mysql-real-escape-string.php#101248
if(is_array($inp))
return array_map(__METHOD__, $inp);
if(!empty($inp) && is_string($inp)) {
return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $inp);
}
return $inp;
}
function validfilter($value,$regexp,$flags='usi') {
if (preg_match('/'.$regexp.'/'.$flags, $value,$result)
&& $result[0]===$value) return $value;
else return false;
}
$charset=array(
'bad'=>'\x00-\x09\x0B\x0C\x0E-\x1F\x80-\xFF',
'sql'=>'\x00\x0a\x0c\x1a\x22\x27\x5c',
'en'=>'a-zA-Z',
'ru'=>'а-яА-ЯёЁ',
'digits'=>'0-9',
'num'=>'0-9-.',
'space'=>'\s',
);
$filter=array(
//login - любой символ, кроме непечатных, sql-"опасных" и пробелов; от 3 до 20 знаков.
'login'=>"^"."([^".$charset['bad'].$charset['sql'].$charset['space']."]{3,20})$",
);
//----------------------
$login = validfilter($_REQUEST['login'],$filter['login']);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question