Home Backend Development PHP Tutorial The difference, function and usage of magic_quotes_gpc and magic_quotes_runtime in PHP_PHP Tutorial

The difference, function and usage of magic_quotes_gpc and magic_quotes_runtime in PHP_PHP Tutorial

Jul 13, 2016 pm 05:44 PM
magic php quotes runtime effect the difference occur and Quote usage of magic

Magic quotes take effect when $_GET, $_POST, $_COOKIE is passed
1.
Condition: magic_quotes_gpc=off
The string written to the database has not been filtered in any way. The string read from the database is not processed in any way.
Data: $data="snow"''sun"; (There are four consecutive single quotes between snow and sun).
Operation: Write the string: "snow"''sun" to the database ,
Result: A sql statement error occurred, mysql could not successfully complete the sql statement, and failed to write to the database.
Database saving format: No data.
Output data format: No data.
Note: Unprocessed single quotes will cause errors in sql statements when written to the database.
2.
Condition: magic_quotes_gpc=off
The string written to the database is processed by the function addlashes(). The string read from the database is not processed in any way.
Data: $data="snow"''sun"; (There are four consecutive single quotes between snow and sun).
Operation: Write the string: "snow"''sun" to the database ,
Result: The sql statement was successfully executed and the data was successfully written into the database
Database saving format: snow”''sun (same as input)
Output data format: snow”''sun (same as input)
Note: The addslashes() function converts single quotes into 'escape characters so that the sql statement can be successfully executed.
But ' is not stored in the database as data. The database saves snow"''sun instead of We imagine snow''''sun
3.
Condition: magic_quotes_gpc=on
The string written to the database is not processed in any way. The string read from the database is not processed in any way. >Data: $data="snow"''sun"; (There are four consecutive single quotes between snow and sun).
Operation: Write the string: "snow"''sun" into the database,
Result: The sql statement was executed smoothly and the data was successfully written into the database
Database saving format: snow”''sun (same as input)
Output data format: snow”''sun (same as input)
Explanation: magic_quotes_gpc=on converts single quotes into 'escape characters so that the sql statement can be successfully executed.
But ' is not entered into the database as data. The database saves snow"''sun instead of the snow we imagined. ''''sun.
4.
Condition: magic_quotes_gpc=on
The string written to the database is processed by the function addlashes(). The string read from the database is not processed in any way.
Data: $data="snow"''sun"; (There are four consecutive single quotes between snow and sun).
Operation: Write the string: "snow"''sun" to the database ,
Result: The sql statement was successfully executed and the data was successfully written into the database
Database saving format: snow''''sun (escape characters added)
Output data format: snow''''sun ( Added escape characters)
Description: magic_quotes_gpc=on converts single quotes into 'escape characters so that the sql statement can be successfully executed.
addslashes converts single quotes about to be written into the database into ', the latter The conversion is written into the
database as data, and the database saves snow''''sun
The summary is as follows:
1. For the case of magic_quotes_gpc=on,
we can not input and output the database String data can be operated by
addslashes() and stripslashes(), and 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 magic_quotes_gpc=off
addslashes() must be used to process the input data, but there is no need to use stripslashes() to format the output
because addslashes() does not include backslashes together Writing to the database just helps mysql complete the execution of the sql statement.
Supplementary:
magic_quotes_gpc scope is: WEB client server; action time: when the request starts, such as when the script is running.
magic_quotes_runtime scope: data read from a file or the result of executing exec() or obtained from a SQL query; action time: every time the script accesses the data generated in the running state
=== ========= The difference and usage of magic_quotes_gpc and magic_quotes_runtime =============
PHP provides two magic reference functions magic_quotes_gpc and magic_quotes_runtime that are convenient for us to quote data. If this function is set to ON in php.ini, it will automatically add backslashes for the data we quote when encountering single quotes ' and double quotes ', and backslashes, helping us automatically translate symbols and ensure data operation. It runs correctly, but under different versions of PHP or different server configurations, some magic_quotes_gpc and magic_quotes_runtime are set to on, and some are set to off, so the program we write must comply with both on and off conditions.So what is the difference between the two functions magic_quotes_gpc and magic_quotes_runtime? See the description below:
magic_quotes_gpc
Scope is: WEB client server;
Action time: The request starts, for example when the script is running.
magic_quotes_runtime
Scope: Data read from a file or the result of executing exec() or obtained from a SQL query;
Time of action: Every time the script accesses data generated in the running state .
So
The setting value of magic_quotes_gpc will affect the data obtained through Get/Post/Cookies
The setting value of magic_quotes_runtime will affect the data read from the file or the data obtained from the database query
Example:
Copy content to clipboard
Code:


STR:

/* We fill in the form: " " These symbols, if magic_quotes_gpc is not turned on, then they will not be backslash escaped*/
echo The value passed through POST now Is: ,$_POST[str],
;

if(get_magic_quotes_gpc()) { // Check whether magic_quotes_gpc is turned on, if not, use addslashes to escape
$str = $_POST[str];
} else {
$str = addslashes($_POST[str]);
}

echo Here is the escaped version: ,$str,


;
$sql = "INSERT INTO lastnames (lastname) VALUES ($str)";

//================ ================================================== ====================
//-----magic_quotes_gpc will only escape: Data obtained through Get/Post/Cookies
// -----magic_quotes_runtime will escape: data read from a file or the result of executing exec() or obtained from a SQL query
//============== ================================================== ======================
$data = implode(file(try.php)); // We still write the characters " , used to test
echo Here is the data of try.php,;
if (get_magic_quotes_runtime()) {
$data = $data;
echo .$data escaped by the system itself. ;
} else {
echo escaped by addslashes.$data = addslashes($data);
}

$sql = "INSERT INTO lastnames (lastname) VALUES ($ data)";
echo
The SQL statement is:
,$sql;
//---It is escaped when entering the database, but there is an extra backslash. When we want to read the original data, use stripslashes() to remove the backslash
//---stripslashes() and addslashes() have opposite effects
?>


The most critical difference is the two points mentioned above: they target different processing objects
The setting value of magic_quotes_gpc will affect the data obtained through Get/Post/Cookies
magic_quotes_runtime The set value will affect the data read from the file or the data obtained from the database query
Here are a few related functions:
set_magic_quotes_runtime():
Set the magic_quotes_runtime value. 0 =Off.1=On. The default state is off. You can view magic_quotes_runtime through echo phpinfo();
get_magic_quotes_gpc():
View magic_quotes_gpc value.0=Off.1=On.
get_magic_quotes_runtime() :
View magic_quotes_runtime value. 0=off. 1=on.
Note that there is no set_magic_quotes_gpc() function, that is, the value of magic_quotes_gpc cannot be set in the program.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478754.htmlTechArticleMagic quotes work when passing $_GET, $_POST, $_COOKIE 1. Condition: magic_quotes_gpc=off write The strings entered into the database are not filtered in any way. String read from database...
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

deepseek What is the difference between r1 and v3 version deepseek What is the difference between r1 and v3 version Feb 19, 2025 pm 03:24 PM

DeepSeek: In-depth comparison between R1 and V3 versions helps you choose the best AI assistant! DeepSeek already has tens of millions of users, and its AI dialogue function has been well received. But are you confused when facing the R1 and V3 versions? This article will explain the differences between the two in detail to help you choose the most suitable version. The core difference between DeepSeekR1 and V3 version: Features The design goal of the V3 version focuses on complex problem reasoning, deep logic analysis, multi-functional large language model, focusing on scalability and efficiency architecture and parameter reinforcement learning optimization architecture, parameter scale 1.5 billion to 70 billion MoE hybrid Expert architecture, total parameters are as high as 671 billion, each token is activated by 37 billion

Summary of FAQs for DeepSeek usage Summary of FAQs for DeepSeek usage Feb 19, 2025 pm 03:45 PM

DeepSeekAI Tool User Guide and FAQ DeepSeek is a powerful AI intelligent tool. This article will answer some common usage questions to help you get started quickly. FAQ: The difference between different access methods: There is no difference in function between web version, App version and API calls, and App is just a wrapper for web version. The local deployment uses a distillation model, which is slightly inferior to the full version of DeepSeek-R1, but the 32-bit model theoretically has 90% full version capability. What is a tavern? SillyTavern is a front-end interface that requires calling the AI ​​model through API or Ollama. What is breaking limit

Does Bitcoin have stocks? Does Bitcoin have equity? Does Bitcoin have stocks? Does Bitcoin have equity? Mar 03, 2025 pm 06:42 PM

The cryptocurrency market is booming, and Bitcoin, as a leader, has attracted the attention of many investors. Many people are curious: Do Bitcoin have stocks? The answer is no. Bitcoin itself is not a stock, but investors can indirectly invest in Bitcoin-related assets through various channels, which will be explained in detail in this article. Alternatives to Bitcoin Investment: Instead of investing directly in Bitcoin, investors can participate in the Bitcoin market by: Bitcoin ETF: This is a fund traded on the stock trading market, whose asset portfolio contains Bitcoin or Bitcoin futures contracts. This is a relatively convenient option for investors who are accustomed to stock investments, without having to hold Bitcoin directly. Bitcoin Mining Company Stocks: These companies' business is Bitcoin mining and holding Bitcoin

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

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 is deepseek? What is deepseek? Feb 19, 2025 pm 03:57 PM

DeepSeek: An AI smart assistant you must not miss! DeepSeek is an AI tool that is popular among users. It has become a must-have software for many users recently. It provides powerful intelligent interactive communication functions, and the following is a detailed introduction to its powerful functions: DeepSeek's core functions: Text processing master: Easily create high-quality copywriting, translate and polish it, and improve your text expression ability. Programming tools: efficiently generate and complete code, quickly understand code logic, and effectively detect and correct code errors, greatly improving programming efficiency. Intelligent interaction expert: Built-in intelligent customer service and smart cockpit functions to provide a convenient interactive experience. Data analysis forecasting experts: supporters

See all articles