


Detailed explanation of the specific methods of preventing SQL injection in PHP_PHP tutorial
An excellent
When it comes to website security, we have to mention SQL Injection. If you have used ASP, you must have a deep understanding of SQL injection. It is understood that PHP has relatively high security. This is because versions below MYSQL4 do not support sub-statements, and when magic_quotes_gpc in php.ini is On.
All ' (single quotes), " (double quotes), (backslashes) and null characters in the submitted variables will be automatically converted into escape characters containing backslashes, causing SQL injection A lot of trouble
Please see clearly: It’s just “trouble”~ This does not mean that PHP prevents SQL injection. The book talks about how to bypass escaping by changing the encoding of the injection statement, such as Convert the SQL statement into ASCII encoding (similar to: char(100,58,92,108,111,99,97,108,104,111,115,116...)), or into hexadecimal encoding, or even other forms of encoding. In this way, escape filtering Then it was bypassed, so how to prevent it:
a. Open magic_quotes_gpc or use the addslashes() function
In the new version of PHP, even if magic_quotes_gpc is turned on, there will be no conflict when using the addslashes() function. However, in order to achieve better version compatibility, it is recommended to check the magic_quotes_gpc status before using the transfer function, or turn it off directly. , the code is as follows:
PHP code to prevent SQL injection
<ol class="dp-xml"> <li class="alt"><span><span>// 去除转义字符 </span></span></li> <li><span>function stripslashes_array($array) { </span></li> <li class="alt"><span>if (is_array($array)) { </span></li> <li> <span>foreach ($array as $</span><span class="attribute">k</span><span> =</span><span class="tag">></span><span> $v) { </span> </li> <li class="alt"><span>$array[$k] = stripslashes_array($v); </span></li> <li><span>} </span></li> <li class="alt"><span>} else if (is_string($array)) { </span></li> <li> <span>$</span><span class="attribute">array</span><span> = </span><span class="attribute-value">stripslashes</span><span>($array); </span> </li> <li class="alt"><span>} </span></li> <li><span>return $array; </span></li> <li class="alt"><span>} </span></li> <li><span>@set_magic_quotes_runtime(0); </span></li> <li class="alt"><span>// 判断 magic_quotes_gpc 状态 </span></li> <li><span>if (@get_magic_quotes_gpc()) { </span></li> <li class="alt"> <span>$</span><span class="attribute">_GET</span><span> = </span><span class="attribute-value">stripslashes_array</span><span>($_GET); </span> </li> <li> <span>$</span><span class="attribute">_POST</span><span> = </span><span class="attribute-value">stripslashes_array</span><span>($_POST); </span> </li> <li class="alt"> <span>$</span><span class="attribute">_COOKIE</span><span> = </span><span class="attribute-value">stripslashes_array</span><span>($_COOKIE); </span> </li> <li><span>} </span></li> </ol>
Remove the escape of magic_quotes_gpc and then use the addslashes function, the code is as follows:
PHP to prevent SQL injection Code
$keywords = addslashes($keywords);
$keywords = str_replace("_","_",$keywords);//Escape "_"
$keywords = str_replace("%","%",$keywords);//Escape "%"
The purpose of the last two str_replace substitutions is to prevent hackers from converting SQL encoding to attack. >
b. Mandatory character format (type)
In many cases we need to use something like xxx.php?id=xxx URL, generally $id is an integer variable. In order to prevent attackers from tampering with $id into attack statements, we must try to force the variable. The code is as follows:
PHP code to prevent SQL injection
$id=intval($_GET['id']);
Of course, there are other variable types. If necessary, try to force the format
<.>c. SQL statements contain variables with quotes
This is very simple, but it is also easy to form a habit. Let’s take a look at these two SQLs first. Statement: SQL codeSELECT * FROM article WHERE articleid='$id'
SELECT * FROM article WHERE articleid=$id
The two writing methods are in each They are common in both programs, but the security is different. In the first sentence, the variable $id is placed in a pair of single quotes, which makes the variables we submit become strings, even if the correct SQL is included. statement will not be executed normally, but the second sentence is different. Since the variables are not put in single quotes, everything we submit will be executed as an SQL statement as long as it contains spaces. Therefore, we have to Get into the habit of quoting variables in SQL statements.
d. URL pseudo-static
URL pseudo-static is also a URL rewriting technology, like Discuz! Similarly, it is a good idea to rewrite all URLs into a format similar to xxx-xxx-x.html, which is beneficial to SEO and achieves a certain level of security. But in order to prevent SQL injection in PHP, the premise is that you must have a certain "regular" foundation.
http://www.bkjia.com/PHPjc/446121.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
