PHP string escaping related functions
Summary of functions related to PHP character escaping. Sometimes for safety reasons, we need to escape the string entered by the user.
There are incorrect or unclear things in the article. Please point it out. ~~~
The configurations and functions related to PHP string escaping are as follows:
1.magic_quotes_runtime
2.magic_quotes_gpc
3.addslashes() and stripslashes()
4.mysql_escape_string()
5.addcslashes() and stripcslashes()
6.htmlentities() and html_entity_decode()
7.htmlspecialchars() and htmlspecialchars_decode()
When magic_quotes_runtime is turned on, most functions of PHP automatically add backslashes to overflow characters in data imported from outside (including databases or files).
You can use set_magic_quotes_runtime() and get_magic_quotes_runtime() to set and detect its status.
Note: These two functions have been deprecated in PHP 5.3.0 or above, which means that this option is turned off in PHP 5.3.0 or above.
magic_quotes_gpc sets whether to automatically escape certain characters in the data transmitted by GPC (GET, POST, COOKIE).
You can use get_magic_quotes_gpc() to detect its setting. .
If this setting is not turned on, you can use the addslashes() function to add to the string to escape
addslashes() Add a backslash before the specified predefined character.
Predefined characters include single quotation mark ('), double quotation mark ("), backslash (\) and NUL (NULL character).
The above is the explanation given by W3SCHOOL.COM.CN that I always intuitively understand Not very accurate
Because it converts single quotes (') into double quotes (") when magic_quotes_sybase=on and converts single quotes (') into (\') when magic_quotes_sybase=off
stripslashes() The function of the function is exactly the opposite of addslashes(). Its function is to remove the escaping effect.
mysql_escape_string() Escapes special characters in strings used in SQL statements.
The special ones here include (\x00), ( \n), ( \r ), (\), ( '), ("), ( \x1a)
addcslashes() The C language style uses backslash to escape characters in a string. This function is rarely used by people, but it should be noted that when choosing to convert characters 0, a, b, f, n, r, t and v When defined, they will be converted to \0, \a, \b, \f, \n, \r, \t and \v. In PHP, only \0 (NULL), \r (carriage return). , \n (newline character) and \t (tab character) are predefined escape sequences, and in C language, all the above converted characters are predefined escape sequences. The same is true for stripcslashes(). The function is to remove its escape.
htmlentities() Convert characters into HTML entities. (What are HTML entities? Google it yourself~~)
Please see here for the inverse function html_entity_decode. () -Convert HTML entities to characters. The
htmlspecialchars() function converts some predefined characters into HTML entities.
These predefined characters are:
& (ampersands). become &
" (double quote) become "
' (single quote) become '
< (less than) become <
> (greater than) become >
Detailed parameters Please see here. The inverse function is htmlspecialchars_decode() to convert some predefined HTML entities into characters.
My own experience:
>>Multiple single quote escapes may cause database problems. Security issues
>> It is not recommended to use mysql_escape_string for escaping. It is recommended to escape when obtaining user input
>> Since set_magic_quotes_runtime() has been abandoned in PHP5.3.0 and later versions , so it is recommended to turn off the unified configuration in previous versions:
The code is as follows:
1 2 3 |
|
>> Magic_quotes_gpc cannot be defined through a function, so it is recommended to unify it on the server Enable, you should make a judgment when writing a program to avoid security issues caused by not opening GPC
When escaping GPC through addslashes, you should pay attention to filtering key values and values when the user submits array data
code show as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
>> 利用在用户输入或输出时候转义HTML实体以防止XSS漏洞的产生!
今天碰到一个处理文件特殊字符的事情,再次注意到这个问题,在php中:
* 以单引号为定界符的php字符串,支持两个转义\'和\\
* 以双引号为定界符的php字符串,支持下列转义:
\n 换行(LF 或 ASCII 字符 0x0A(10))
\r 回车(CR 或 ASCII 字符 0x0D(13))
\t 水平制表符(HT 或 ASCII 字符 0x09(9))
\\ 反斜线
\$ 美元符号
\" 双引号
\[0-7]{1,3} 此正则表达式序列匹配一个用八进制符号表示的字符
\x[0-9A-Fa-f]{1,2} 此正则表达式序列匹配一个用十六进制符号表示的字符
举几个例子:
一个包含\0特殊字符的例子:
1 2 3 4 5 |
|
输出结果:
----------------------
9
102 102 102 102 0 102 102 102 102
替换特殊字符的例子
1 2 3 4 5 6 7 8 9 |
|
----------------------
8
102 102 102 102 102 102 102 102
八进制ascii码例子:
1 2 3 4 5 6 |
|
输出结果:
----------------------
11
0 1 2 3 7 8 9 0 56 92 56
十六进制ascii码例子:
1 2 3 4 5 |
|
输出结果:
----------------------
10
0 1 2 3 7 8 9 16 17 255
The above is the detailed content of PHP string escaping related functions. For more information, please follow other related articles on the PHP Chinese website!

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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

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.
