I have talked about a lot of SQL injection prevention codes before, but we still have to start with our server script. Let’s talk about some common methods of preventing injection in PHP for your reference.
The most common one is probably
First set magic_quotes_gpc to On and display_errors to Off. If it is an id type, we use intval() to convert it to an integer type, as shown in the code:
$id=intval($id);
Okay, let me introduce the basic principles of PHP submission data filtering
1) When submitting variables into the database, we must use addslashes() for filtering. For example, our injection problem can be solved with just one addslashes(). In fact, when it comes to variable values, the intval() function is also a good choice for filtering strings.
2) Enable magic_quotes_gpc and magic_quotes_runtime in php.ini. magic_quotes_gpc can change the quotation marks in get, post, and cookie into slashes. magic_quotes_runtime can play a formatting role in data entering and exiting the database. In fact, this parameter has been very popular since the old days when injection was crazy.
3) When using system functions, you must use escapeshellarg(), escapeshellcmd() parameters to filter, so that you can use system functions with confidence.
4) For cross-site, both parameters of strip_tags() and htmlspecialchars() are good. All tags with html and php submitted by users will be converted. For example, angle brackets "<" will be converted into harmless characters such as "<".
The code is as follows | Copy code | ||||||||
$new = htmlspecialchars("Test", ENT_QUOTES);
5) Regarding the filtering of related functions, just like the previous include(), unlink, fopen(), etc., as long as you specify the variables you want to perform the operation or strictly filter the related characters, I think this will be enough. Impeccable. 2. Simple data filtering with PHP 1) Storage: trim($str),addslashes($str) Share an example Specific code:
This code then loads this function on all pages. In this way, when filtering, I find that there seems to be a problem when uploading files. source:php.cn
Previous article:PHP anti-injection configuration and php anti-injection code_PHP tutorial
Next article:PHP verification code generation program code_PHP tutorial
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|