Home > Article > Backend Development > SQL injection and escaped php function code_PHP tutorial
sql injection:
Under normal circumstances:
delete.php?id=3;
$sql = 'delete from news where id = '.$_GET['id'];
Malicious situation:
delete.php?id=3 or 1;
$sql = 'delete from news where id = 3 or 1'; -------After executing this, all records will be deleted
Relevant measures should be taken. . . For example, check whether it is a number before using it, etc.
Convince yourself that information from the client is never reliable! !
Escape:
Sometimes the data transmitted from the client may maliciously contain some special characters, such as single quotes, slashes, etc., so it needs to be escaped and converted into ordinary characters. In this case, string addslashes ( string $str ), this function can escape a variable. However, if you want to escape the elements in the array, use foreach to loop the array, as follows:
on failure
That is to say: when using a custom function, it must be able to receive at least two parameters, while addslashes() can only receive one parameter, so customize a function as follows:
Copy code
The code is as follows:
After the magic quotes are turned on, the system will automatically escape the $_GET, $_POST, $_COOKIE data. If you do it manually again without knowing it, it will be too much. To do it reasonably, To escape, you must first determine whether the magic symbol has been turned on. Use magic_quotes_gpc() to determine. There is no need to pass a value. Close returns 0 and close returns 1
Copy code
The code is as follows:
array_walk_recursive(&$_COOKIE,'_addslashes');
}