Home Backend Development PHP Tutorial Differences and usage introduction based on magic_quotes_gpc and magic_quotes_runtime_PHP tutorial

Differences and usage introduction based on magic_quotes_gpc and magic_quotes_runtime_PHP tutorial

Jul 21, 2016 pm 03:11 PM
magic quotes runtime and introduce use the difference based on data of

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() < '5.3.0') {
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 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)

What are the basic requirements for c language functions What are the basic requirements for c language functions Apr 03, 2025 pm 10:06 PM

C language functions are the basis for code modularization and program building. They consist of declarations (function headers) and definitions (function bodies). C language uses values ​​to pass parameters by default, but external variables can also be modified using address pass. Functions can have or have no return value, and the return value type must be consistent with the declaration. Function naming should be clear and easy to understand, using camel or underscore nomenclature. Follow the single responsibility principle and keep the function simplicity to improve maintainability and readability.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

How to set password protection for export PDF on PS How to set password protection for export PDF on PS Apr 06, 2025 pm 04:45 PM

Export password-protected PDF in Photoshop: Open the image file. Click "File"&gt; "Export"&gt; "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

Concept of c language function Concept of c language function Apr 03, 2025 pm 10:09 PM

C language functions are reusable code blocks. They receive input, perform operations, and return results, which modularly improves reusability and reduces complexity. The internal mechanism of the function includes parameter passing, function execution, and return values. The entire process involves optimization such as function inline. A good function is written following the principle of single responsibility, small number of parameters, naming specifications, and error handling. Pointers combined with functions can achieve more powerful functions, such as modifying external variable values. Function pointers pass functions as parameters or store addresses, and are used to implement dynamic calls to functions. Understanding function features and techniques is the key to writing efficient, maintainable, and easy to understand C programs.

Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Apr 05, 2025 pm 01:03 PM

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

What are the differences and connections between c and c#? What are the differences and connections between c and c#? Apr 03, 2025 pm 10:36 PM

Although C and C# have similarities, they are completely different: C is a process-oriented, manual memory management, and platform-dependent language used for system programming; C# is an object-oriented, garbage collection, and platform-independent language used for desktop, web application and game development.

How to use XPath to search from a specified DOM node in JavaScript? How to use XPath to search from a specified DOM node in JavaScript? Apr 04, 2025 pm 11:15 PM

Detailed explanation of XPath search method under DOM nodes In JavaScript, we often need to find specific nodes from the DOM tree based on XPath expressions. If you need to...

What are the different ways of promoting H5 and mini programs? What are the different ways of promoting H5 and mini programs? Apr 06, 2025 am 11:03 AM

There are differences in the promotion methods of H5 and mini programs: platform dependence: H5 depends on the browser, and mini programs rely on specific platforms (such as WeChat). User experience: The H5 experience is poor, and the mini program provides a smooth experience similar to native applications. Communication method: H5 is spread through links, and mini programs are shared or searched through the platform. H5 promotion methods: social sharing, email marketing, QR code, SEO, paid advertising. Mini program promotion methods: platform promotion, social sharing, offline promotion, ASO, cooperation with other platforms.

See all articles