search
HomeBackend DevelopmentPHP TutorialSome usages of magic_quotes_gpc function in php_PHP tutorial

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
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

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

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)