search
HomeBackend DevelopmentPHP TutorialDifferences and usage introduction based on magic_quotes_gpc and magic_quotes_runtime_PHP tutorial

It is very useful when there are some characters like " " '
in your data that you want to write into the database and do not want to be filtered out. It will be automatically added in front of these characters, such as
China's Big Land and Natural Resources "Haha"
China\Big Land and Natural Resources "Haha"
can be turned off using set_magic_quotes_runtime(0). Of course, you can also set it directly in php.ini
get_magic_quotes_runtime() to obtain the PHP environment variable. The value of magic_quotes_runtime.

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, because this Will result in double-level escaping. When encountering this situation, you can use the function get_magic_quotes_gpc() to detect the difference between the two.
set_magic_quotes_runtime() allows programmers to dynamically turn on or off magic_quotes_runtime in code,
set_magic_quotes_runtime(1) means turning it on, and set_magic_quotes_runtime(0) means turning it off. When set_magic_quotes_runtime(1) is used, the text read from the database or through functions such as fread will automatically escape ' " and backslashes to prevent overflow. This is when transferring data from the database Very useful. But in general, it should be turned off, otherwise single quotes, double quotes and backslashes will be added to the data read from the database, causing abnormal display in public files. Add the sentence set_magic_quotes_runtime(0); to the header to force close magic_quotes_runtime

.

magic_quotes_gpc

Scope of action is: WEB client server;

Time of action: The request starts, for example, when the script is running.

magic_quotes_runtime

Scope of action: 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,

magic_quotes_gpc escapes the data passed through GET, POST, and COOKIE. Generally, it must be escaped before the data is stored in the database.

magic_quotes_gpc cannot be dynamically turned on or off in the code. You need to set magic_quotes_gpc to php.ini. on or off,
You can use get_magic_quotes_gpc to get the status of magic_quotes_gpc in the code.
When magic_quotes_gpc is off, you need to manually addlashes the data. The code is as follows:



Copy the code

The code is as follows:if (!get_magic_quotes_gpc()) {
new_addslashes($_GET);
new_addslashes($_POST);
new_addslashes($_COOKIE);
}

function new_addslashes($ string) {
if (is_array($string)) {
foreach ($string as $key => $value) {
$string[$key] = new_addslashes($value);
                                                                                                                   >


Copy code

The code is as follows:


$data1 = $_POST['aaa'];
$data2 = implode(file ('1.txt'));

if (get_magic_quotes_gpc()) {
//Write the data $data1 directly into the database
} else { $data1 = addslashes($ data1); //Write data $data1 directly into the database }
if (get_magic_quotes_runtime()){
//Write data $data2 directly into the database
// The data read from the database will be output after stripslashes() once
} else {
$data2 = addslashes($data2);
//Write the data $data2 into the database
//From The data read from the database is directly output
}

++++++++++++++++++++++++++++++++++++++++++++ +++++++

Experience summary:

1. For GPC, regardless of whether magic_quotes_gpc is turned on in the system (that is, magic_quotes_gpc = On in php.ini), we turn on magic_quotes_gpc uniformly and escape the contents of get, post, and cookie. . The operation is as follows:
(extracted from uchome system)

Copy code The code is as follows:

function saddslashes($string) {
if (is_array($string)) {
foreach ($string as $key => $val) {
$string[$key] = saddslashes($val);
}
} else {
                                                         );
if(empty($magic_quote)) {
$_GET = saddslashes($_GET);
$_POST = saddslashes($_POST);
}

// COOKIE, escape the cookie value
$prelength = strlen($_SC['cookiepre']);
foreach ($_COOKIE as $key => $val) {
if(substr($key , 0, $prelength) == $_SC['cookiepre']) {
$_SCOOKIE[(substr($key, $prelength))] = empty($magic_quote) ? saddslashes($val) : $val;
}  
}


2. For magic_quotes_runtime, we turn it off uniformly, that is, set_magic_quotes_runtime(0); do not allow single quotes, double quotes and inverse quotes of data read from the database Slashes are added automatically. In this way, the operations on the database are as follows: before adding data to the database, we manually perform addslashes() on the data, and when fetching data from the database, the opposite operation is performed, that is, stripslashes().

3. For the content to be serialized, keep the bare data, that is, remove the escape, stripslashes(), and then save the serialized content to the database (note that the serialized content Without single quotes ('), double quotes ("), or backslashes ()), the example is as follows:
$feedarr['body_data'] = serialize(stripslashes($body_data));
++++++++++++++++++++++++++++++++++++++++++++ +++++++++++

Is there a problem with Function set_magic_quotes_runtime() is deprecated?

Deprecated: Function set_magic_quotes_runtime() is deprecated error occurred when installing PHPCMS. I checked the network and information and found that the set_magic_quotes_runtime() function was removed after PHP5.3 and PHP6.0. I can use the following alternative:

view sourceprint?

@set_magic_quotes_runtime(0);

or

view sourceprint?

ini_set("magic_quotes_runtime", 0);

or

view sourceprint?

if (phpversion() set_magic_quotes_runtime(0);

}




http://www.bkjia.com/PHPjc/326823.html

www.bkjia.com

true

TechArticleWhen there are some characters like " ' in your data, you want to write them into the database, but you don't want to be filtered. It is very useful when it is dropped, it will be automatically added before these characters, such as Chinese...
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

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.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)