Home Backend Development PHP Tutorial what are magic quotes in php

what are magic quotes in php

May 30, 2019 am 11:28 AM

what are magic quotes in php

What are magic quotes:

Magic quotes are used by the program to automatically convert the data entering the PHP script process. When turned on, all ' (single quote), " (double quote), \ (backslash) and NULL characters will be automatically escaped with a backslash. It has the same effect as the addslashes() function.

Magic quote directive:

magic_quotes_gpc affects HTTP request data (GET, POST and COOKIE). Cannot be changed at runtime. The default value in PHP is on. See get_magic_quotes_gpc().

magic_quotes_runtime If turned on, most functions that retrieve and return data from external sources, including databases and text files, will return backslash-escaped data. This option Can be changed at runtime, the default value in PHP is off. See set_magic_quotes_runtime() and get_magic_quotes_runtime().

magic_quotes_sybase If turned on, single quotes will be used to escape single quotes rather than reverse them. Slash. This option will completely override magic_quotes_gpc. If both options are turned on at the same time, single quotes will be escaped into ''. Double quotes, backslashes and NULL characters will not be escaped. How to get its value See ini_get().

The role of magic quotes:

The original introduction of magic quotes was a security consideration to prevent SQL injection. It can help PHP newbies unknowingly Relatively safer code is written in the code, but today, programmers are already well aware of this security issue, and eventually use the database transfer mechanism or prepared statements to replace the magic quotation mark function.

Magic Quotation mark defects:

Portability: When programming, it is considered that opening or closing them will affect portability. You can use get_magic_quotes_gpc() to check whether it is opened and program accordingly.

Performance: Since not every piece of escaped data must be inserted into the database, if all the data entering PHP is escaped, it will have a certain impact on the execution efficiency of the program. Call the escape function at runtime (such as addslashes()) is more efficient. Although php.ini-dist turns this option on by default, php.ini-recommended turns it off by default, mainly for performance reasons.

Inconvenience: Since not all data needs to be escaped, it is annoying to see escaped data in places that do not need to be escaped. For example, if you send an email through a form, you will see a lot of \'. To solve this problem, you can use stripslashes () function processing.

Switch magic quotes:

magic_quotes_gpc cannot be set through ini_set(). There are three ways to set magic_quotes_gpc.

1. Modify the PHP configuration file php.ini. This method requires administrative rights on the server to modify it. If it is just a virtual space, you can only use the latter two methods.

; Magic quotes
; Magic quotes for incoming GET/POST/Cookie data.
magic_quotes_gpc = Off
; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc.
magic_quotes_runtime = Off
Use Sybase-style magic quotes (escape ' with '' instead of \').
Magic_quotes_sybase = Off
Copy after login

2. Set in htaccess. This can only be used if the server supports htaccess.

php_flag magic_quotes_gpc Off
Copy after login

3. Shield in the code. This method is more portable, but has the lowest efficiency, so it is best to turn off magic_quotes_gpc by modifying the configuration article when you have server management rights.

Example code:

<?php
if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value)
    {
        $value = is_array($value) ?
                    array_map(&#39;stripslashes_deep&#39;, $value) :
                    stripslashes($value);
        return $value;
    }
    $_POST = array_map(&#39;stripslashes_deep&#39;, $_POST);
    $_GET = array_map(&#39;stripslashes_deep&#39;, $_GET);
    $_COOKIE = array_map(&#39;stripslashes_deep&#39;, $_COOKIE);
    $_REQUEST = array_map(&#39;stripslashes_deep&#39;, $_REQUEST);
}
?>
Copy after login

Summary:

Magic quotes were originally introduced to prevent SQL injection, which is beneficial to developers. Friend, but it also brings a lot of inconvenience when using it. Now there are more and better alternatives, so if you are still developing in php 5.3.0 or a version before php 5.3.0, you should try to avoid using magic quotes. , has been removed since PHP 5.4.0.

The above is the detailed content of what are magic quotes in php. For more information, please follow other related articles on the PHP Chinese website!

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles