Understanding and analysis of phpmagic_quotes_gpc _ PHP Tutorial

Source: Internet
Author: User
Understanding and analysis of phpmagic_quotes_gpc. "The process of learning is to constantly discover and correct errors! For average people, the first two paragraphs can be viewed. MagicQuot blkyao says, "the learning process is to constantly discover and correct errors ";

Let's take a look at what I said in the manual!

For average people, just look at the first two paragraphs.

Magic Quotes

Code:
Magic Quotes is a process that automagically 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 ctictives:
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 ''. 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 already CED.

2 Convenience

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


Why not to use Magic Quotes




1 Portability

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

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 ctictives by default, php. ini-recommended disables it. This recommendation is mainly due to performance reasons.
3 Inconvenience

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 need patience like me (not to say that I am patient, but that I am poor at English, only the first two paragraphs can be viewed by ordinary people, especially the words marked with red !!!

In addition, it is important to note that magic references are used when passing $ _ GET, $ _ POST, $ _ COOKIE

The following is a case

Code:
1.
Condition: magic_quotes_gpc = off
The string written to the database has not been filtered. The strings read from the database are not processed.

Data: bytes $ data = "snow '''sun"; (four consecutive single quotes between snow and sun ).

Operation: write the string "snow ''' sun" to the database,

Result: if an SQL statement error occurs, mysql cannot complete the SQL statement successfully and fails to write data to the database.

Database storage format: no data.

Output data format: no data.

Note: an SQL statement error occurs when unprocessed single quotes are written to the database.

Code:
2.
Condition: magic_quotes_gpc = off
The string written to the database is processed by the addslashes () function. Strings read from the database are not processed.

Data: bytes $ data = "snow '''sun"; (four consecutive single quotes between snow and sun ).

Operation: write the string "snow ''' sun" to the database,

Result: the SQL statement is successfully executed and data is successfully written to the database.

Database storage format: snow ''' sun (same as input)

Output data format: snow ''' sun (same as input)

Note: The addslashes () function converts single quotation marks to escape characters of \ 'so that the SQL statement is successfully executed,
However, \ 'is not stored in the database as data. the database stores snow''' sun, not the snow \' \ 'Sun.

Code:
3.
Condition: magic_quotes_gpc = on
The string written to the database has not been processed. Strings read from the database are not processed.

Data: bytes $ data = "snow '''sun"; (four consecutive single quotes between snow and sun ).

Operation: write the string "snow ''' sun" to the database,

Result: the SQL statement is successfully executed and data is successfully written to the database.

Database storage format: snow ''' sun (same as input)

Output data format: snow ''' sun (same as input)

Note: magic_quotes_gpc = on converts single quotes to \ 'escape characters so that the SQL statement is successfully executed,
However, \ 'is not used as data into the database. the database stores snow''' sun, rather than snow \' \ 'Sun.

Code:
4.
Condition: magic_quotes_gpc = on
The string written to the database is processed by the addlashes () function. Strings read from the database are not processed.

Data: bytes $ data = "snow '''sun"; (four consecutive single quotes between snow and sun ).

Operation: write the string "snow ''' sun" to the database,

Result: the SQL statement is successfully executed and data is successfully written to the database.

Database storage format: snow \ 'sun (escape characters added)

Output data format: snow \ 'sun (escape characters added)

Note: magic_quotes_gpc = on converts single quotes to \ 'escape characters so that the SQL statement is successfully executed,
Addslashes converts the single quotation marks (\) to be written into the database, and the latter conversion is written as data.
Database. the database stores snow \ '\ 'Sun
Summary:

1. for magic_quotes_gpc = on,

We may not use the string data of the input or output database
The operation of addslashes () and stripslashes () will also display the data normally.

If you perform addslashes () processing on the input data,
In this case, you must use stripslashes () to remove unnecessary backslash.

2. 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 backslash together into the database, it only helps mysql to complete SQL statement execution.

Supplement:

Magic_quotes_gpc: WEB client server; TIME: When the request starts, for example, when the script is running.
Magic_quotes_runtime: The data read from the file, the exec () execution result, or the result obtained from the SQL query. function Time: The data generated every time the script accesses the running state.

Http://www.bkjia.com/PHPjc/319284.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/319284.htmlTechArticleblankyao said "the process of learning is constantly found errors, constantly correct errors"; first look at the manual on how to say it! For average people, you can take a look at the first two paragraphs as Magic Quot...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.