Home Backend Development PHP Tutorial Some usages of magic_quotes_gpc function in php_PHP tutorial

Some usages of magic_quotes_gpc function in php_PHP tutorial

Jul 20, 2016 am 10:59 AM
magic p php quotes function method yes usage

The magic_quotes_gpc method is based on your php.ini configuration. It is generated if magic_quotes_gpc is turned on. Its function is the same as addslashes. Let me introduce the usage of magic_quotes_gpc in detail. ​

After reading part of the thinksaas source code, I found that the data processing method from $_POST/$_GET is carried out through the function Add_S(), that is, magic_quotes_gpc is not enabled by default in the environment, so the submitted data is processed by addslashes().

I have always been confused about magic_quotes_gpc. I have also posted an article about magic_quotes_gpc before called "The correct relationship between magic_quotes_gpc and addslashes()?" 》, I’m going to talk about this issue again now because I want to understand this thing thoroughly. I have submitted this question on the thinksaas official website and am waiting for the reply. I will update the results to this article at that time.
Question 1: If the data in the data is to be read now, does stripslashes() need to be processed after reading to restore it to the original data state?

Question 2: I see that many other programs handle it in reverse, that is, if magic_quotes_gpc is turned on in the environment, stripslashes() is performed on the submitted data, and then htmlspecialchars() is performed on the data to replace those Regarding special symbols, I would like to ask which method is better, this method or the thinksaas method? I heard that magic_quotes_gpc will not be enabled by default in the future.

Typecho locomotive publishing interface, I use the method in question 2 to process the posted data. I don’t know if it is the best method?

Perform stripslashes() on the submitted data, and then perform htmlspecialchars() on the data - I don’t think this method has any advantages. Still better than TS. If it is a special website, such as Weibo or the like, with few formats, I think it would be best to just addlashed() and then store it directly in the database.
No one answered question 1, but I can answer it myself here. Regardless of whether magic_quotes_gpc is turned on or not, there is no need to perform stripslashes() processing after reading the data, because the data is not added with extra backslashes when saving.

magic_quotes_gpc summary

1. Processing method

Method 1: If magic_quotes_gpc is not enabled in the system environment, perform addslashes() processing on the submitted data.
Method 2: If magic_quotes_gpc is enabled in the system environment, perform stripslashes() processing on the submitted data, and finally perform htmlspecialchars() processing on the data to remove those special symbols.

2. The best way is as the brother said. For simple storage, just addlashed() and then store it. If you need to perform more complex processing on the string before storing it, you generally need to first Remove the backslash automatically added by magic_quotes_gpc, and then process the string. After processing, process it with addslashed() or htmlspecialchars(), and finally store it in the database. Although this is generally the case, methods must be adopted flexibly based on actual conditions.

Updated on 2012-10-21

The best way is to remove the backslash automatically added by magic_quotes_gpc, and then in the database operation class, addlashed() first and then add it to the database


Now let’s see what the official operation says


Let’s take a look at what the manual says first!

For most people, just read the first two paragraphs

Magic Quotes

Code:
Magic Quotes is a process that automatically escapes incoming data to the PHP script. It's preferred to code with magic quotes off and to instead escape the data at runtime, as needed.
What are Magic Quotes


Code:
When on, all ' (single-quote), " (double quote), (backslash) and NULL characters are escaped with a backslash automatically. This is identical to what addslashes() does.

There are three magic quote directives:
magic_quotes_gpc

Code:
Affects HTTP Request data (GET, POST, and COOKIE). Cannot be set at runtime, and defaults to on in PHP.
magic_quotes_runtime

Code:
If enabled, most functions that return data from an external source, including databases and text files, will have quotes escaped with a backslash. Can be set at runtime, and defaults to off in PHP.
magic_quotes_sybase

Code:
If enabled, a single-quote is escaped with a single-quote instead of a backslash. If on, it completely overrides magic_quotes_gpc. Having both directives enabled means only single quotes are escaped as ''. Double quotes, backslashes and NULL's will remain untouched and unescaped.
Why use Magic Quotes


1 Useful for beginners

Magic quotes are implemented in PHP to help code written by beginners from being dangerous. Although SQL Injection is still possible with magic quotes on, the risk is reduced.

2Convenience

For inserting data into a database, magic quotes essentially runs addslashes() on all Get, Post, and Cookie data, and does so automatically.


Why not to use Magic Quotes


1Portability

Code:
Assuming it to be on, or off, affects portability. Use get_magic_quotes_gpc() to check for this, and code accordingly.
2Performance

Code:
Because not every piece of escaped data is inserted into a database, there is a performance loss for escaping all this data. Simply calling on the escaping functions (like addslashes()) at runtime is more efficient.

Although php.ini-dist enables these directives by default, php.ini-recommended disables it. This recommendation is mainly due to performance reasons.
3Inconvenience

Code:
Because not all data needs escaping, it's often annoying to see escaped data where it shouldn't be. For example, emailing from a form, and seeing a bunch of ' within the email. To fix, this may require excessive use of stripslashes( ).
These English words really require people like me to have enough patience (not that I have patience, but that my English is bad). As I said just now, for ordinary people, just read the first two paragraphs, especially when I use The words highlighted in red! ! !

Example

get_magic_quotes_gpc

Get the value of PHP environment variable magic_quotes_gpc.

Syntax: long get_magic_quotes_gpc(void);

Return value: long integer

Function type: PHP system function

Content Description


This function obtains the value of the variable magic_quotes_gpc (GPC, Get/Post/Cookie) set in the PHP environment. Returning 0 means turning off this function; returning 1 means turning this function on. When magic_quotes_gpc is enabled, all ' (single quote), " (double quote), '' (backslash) and null characters will be automatically converted to overflow characters containing backslash.

addslashes -- Use backslashes to quote strings

Description
string addslashes (string str)

Returns a string with backslashes added in front of certain characters for the purpose of database query statements, etc. These characters are single quote ('), double quote ("), backslash ('') and NUL (NULL character).

An example of using addslashes() is when you are entering data into a database. For example, inserting the name O'reilly into the database requires escaping it. Most databases use '' as the escape character: O'''reilly. This puts the data into the database without inserting extra '''s. When the PHP directive magic_quotes_sybase is set to on, it means that inserting ' will be escaped with '.

By default, the PHP instruction magic_quotes_gpc is on, which mainly automatically runs addslashes() on all GET, POST and COOKIE data. Do not use addslashes() on strings that have been escaped by magic_quotes_gpc, as this will result in double escaping. When encountering this situation, you can use the function get_magic_quotes_gpc() to detect it.

Example 1. addslashes() example

The code is as follows Copy code
 代码如下 复制代码

$str = "Is your name O'reilly?";

// 输出:Is your name O'''reilly?
echo addslashes($str);
?>

$str = "Is your name O'reilly?";


// Output: Is your name O'''reilly?

echo addslashes($str);
 代码如下 复制代码

function html($str) {
$str = get_magic_quotes_gpc()?$str:addslashes($str);
return $str;
}

?>
get_magic_quotes_gpc() This function obtains the value of the variable magic_quotes_gpc (GPC, Get/Post/Cookie) in the PHP environment configuration. Returning 0 means turning off this function; returning 1 means turning this function on. When magic_quotes_gpc is turned on, all ' (single quote), " (double quote), '' (backslash) and null characters will automatically be converted to overflow characters containing backslash.
The code is as follows Copy code
function html($str) { $str = get_magic_quotes_gpc()?$str:addslashes($str); return $str; }

The summary is as follows:

1. For PHP magic_quotes_gpc=on,

We can not do anything with the string data of the input and output databases
For the operations of addslashes() and stripslashes(), the data will be displayed normally.

If you perform addslashes() on the input data at this time,
Then you must use stripslashes() to remove excess backslashes when outputting.

2. For the case of PHP magic_quotes_gpc=off

You must use addslashes() to process the input data, but you do not need to use stripslashes() to format the output
Because addslashes() does not write the backslashes into the database, it just helps mysql complete the execution of the sql statement.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445614.htmlTechArticlemagic_quotes_gpc method is based on your php.ini configuration. If magic_quotes_gpc is turned on, it will be generated. Its function is the same as addslashes are the same, let me introduce in detail about magic_...
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1660
14
PHP Tutorial
1260
29
C# Tutorial
1233
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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 in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

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.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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

See all articles