search
HomeBackend DevelopmentPHP TutorialIntroduction to php functions to prevent sql injection

A few days ago, people were injected into the website. Now I will introduce to you several built-in processing functions of PHP to prevent SQL injection. For example, PHP's MySQL operation functions include addslashes(), mysql_real_escape_string(), mysql_escape_string() and other functions.

Specific usage: addslashes prevent SQL injection

Although many domestic PHP programmers still rely on addslashes to prevent SQL injection, it is still recommended that everyone strengthen the check to prevent SQL injection in Chinese. The problem with addslashes is that hackers can use 0xbf27 to replace single quotes, while addslashes only changes 0xbf27 to 0xbf5c27, which becomes a valid multi-byte character. 0xbf5c is still regarded as a single quote, so addslashes cannot successfully intercept.

Of course, addslashes is not useless. It is used for processing single-byte strings. For multi-byte characters, use mysql_real_escape_string.

In addition, for the example of get_magic_quotes_gpc in the PHP manual, the code is as follows:

Function post_check($post) {

$post = addslashes($post); // Correct the case where magic_quotes_gpc is not turned on Filtering of submitted data

}

$post = str_replace("_", "_", $post); // Filter out '_'

$post = str_replace("%", "%", $ post); // Filter out ' % '                                                                                                                                              ​

}//Open source code phpfensi.com

?>

//Or

function inject_check($sql_str)

{                                                            .             . . '|

Function verify_id($id=null) {

(inject_check($id) ) { exit('The submitted parameters are illegal! ');} // Injecting judgment

Elseif (! IS_NUMERIC ($ ID)) {exit (' The parameters submitted illegal! ');} // Digital judgment

$ ID = intval ($ ID); // Tolerance Typed

                                                                                                                                             Escape special characters in unescaped_string, taking into account the connection's current character set, so it can be safely used in mysql_query().

Note: mysql_real_escape_string() does not escape % and _.

mysql_real_escape_string,Example#1 mysql_real_escape_string() example, the code is as follows:

$item = "Zak's and Derick's Laptop" ;

$escaped_item = mysql_real_escape_string ( $item );

printf ( "Escaped string: %sn" , $escaped_item );

?& gt;

//Above examples The following output will be produced:

//Escaped string: Zak's and Derick's Laptop

mysql_escape_string

This function escapes unescaped_string so that it can be safely used in mysql_query().

Note: mysql_escape_string() does not escape % and _. This function is exactly the same as mysql_real_escape_string(), except that mysql_real_escape_string() accepts a connection handle and transfers the string according to the current character set. mysql_escape_string() does not accept connection parameters and does not care about the current character set setting.

Example 1. mysql_escape_string() example, the code is as follows:

$item = "Zak's Laptop";

$escaped_item = mysql_escape_string($item);

printf ("Escaped string: %sn", $escaped_item);

?>

//Output:

//Escaped string: Zak's Laptop

mysql_ real_escape_string and mysql_escape_string these 2 The difference between the two functions:

mysql_real_escape_string must be used under (PHP 4 >= 4.3.0, PHP 5), otherwise mysql_escape_string can only be used. The difference between the two is: mysql_real_escape_string takes into account the current character set of the connection, And mysql_escape_string is not considered.

We can use judgment to process it comprehensively. The code is as follows:

function cleanuserinput($dirty){

if (get_magic_quotes_gpc()) {

$clean = mysql_real_escape_string(stripslashes($dirty)) ;

}else{

$clean = mysql_real_escape_string($dirty);

}

return $clean;

}

To summarize: * addslashes() is a forced addition; () will determine the character set, However, there are requirements for the PHP version; * mysql_escape_string does not consider the current character set of the connection.


Statement
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
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.