Home Backend Development PHP Tutorial A little understanding and analysis of quotes php magic_quotes_gpc

A little understanding and analysis of quotes php magic_quotes_gpc

Jul 29, 2016 am 08:38 AM
quotes

blankyao said, “The process of learning is to constantly discover mistakes and constantly correct them”;
Let’s read what the manual says first!
For ordinary 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
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 directives 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 require people like me to have enough patience (not that I have patience, but my English is bad). As I said just now, for ordinary people, just read the first two paragraphs, especially The words I highlighted in red! ! !
In addition, special attention is paid to the fact that magic quotes take effect when passing $_GET, $_POST, $_COOKIE
The following is the case
Code:
1.
Condition: magic_quotes_gpc=off
The string written to the database has not passed any Filtration processing. 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: 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 SQL statement errors when written to the database.
Code:
2.
Condition: magic_quotes_gpc=off
The string written to the database is processed by the function addslashes(). 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 successfully executed and the data was successfully written to the database
Database saving format: snow''''sun (same as input)
Output data format: snow''''sun (same as input)
Instructions: addslashes() The function converts single quotes into 'escape characters so that the sql statement can be successfully executed.
But ' is not stored as data in the database. The database saves snow''''sun and not the snow''''sun we imagined.
Code:
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 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)
Description: magic_quotes_gpc=on Converting single quotes to ' escape characters allows the sql statement to be executed successfully,
but ' is not entered into the database as data, and the database saves snow''''sun instead of the snow''''sun we imagined.
Code:
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" into the database,
Result: The sql statement was executed smoothly and the data was successfully written into the database.
Database saving format: snow''''sun (with escape characters added)
Output data format: snow''''sun (with escape characters added)
Description : magic_quotes_gpc=on converts single quotes into 'escape characters so that the sql statement can be successfully executed.
addslashes converts the single quotes that are about to be written into the database into '. The latter conversion is written into the database as data and is saved in the database. It is snow''''sun
The summary is as follows:
1. For the case of magic_quotes_gpc=on,
We can not perform
addslashes() and stripslashes() operations on the string data of the input and output database, 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
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 together into the database, it is just a help mysql completes the execution of the sql statement.
Supplement:
magic_quotes_gpc Scope of action 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; Time of action: Every time the script accesses data generated in the running state
The above introduces some understanding and analysis of quotes php magic_quotes_gpc, including the content of quotes. I hope it will be helpful to friends who are interested in PHP tutorials.

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
3 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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles