PHP to add a backslash in front of the reason and PHP to remove the backslash method, three ways to close the PHP magic quotes _php Tutorial

Source: Internet
Author: User
The server space provided by the general Space quotient default PHP directive MAGIC_QUOTES_GPC is on, that is, open. You can then use the stripslashes () function to remove the auto-added backslash. The usage is: for example, the variable containing the string is $STR, then use the stripslashes () function to process the string: stripslashes ($STR), the result of the output is to remove the backslash.

If the result of the output contains a backslash, the output can be processed with the stripslashes () function, that is, $str=stripslashes ($STR), save to remove the backslash contained in the output content.

However, there is another problem, that is, because the local PHP command MAGIC_QUOTES_GPC is off, if you use this function, it will be the normal backslash also removed. This is not what we want.

The solution is to use the function GET_MAGIC_QUOTES_GPC () to detect, if it is open, then remove the backslash, if the state is closed, do not remove the backslash.

The program code is as follows:

$str =$_post["str"];           Read the contents of STR to the $STR variable if (GET_MAGIC_QUOTES_GPC ()) {   //if GET_MAGIC_QUOTES_GPC () is open    $str =stripslashes ($STR);  The string is processed}

This article was revised on April 25, 2012 10:08:03 as follows:

Here are three ways to solve this problem:

1 modifying PHP configuration Files php.ini

This method is only suitable for the case of having the right to administer the server, if the virtual space is used, then only the latter two methods can be used.

Set MAGIC_QUOTES_GPC, Magic_quotes_runtime, magic_quotes_sybase all to off in PHP config file php.ini. As shown below:

MAGIC_QUOTES_GPC = off Magic_quotes_runtime = off magic_quotes_sybase = off

2 using the. htaccess file

This method is supported only in cases where the server supports htaccess, and now the server will generally support

In the program directory, add the following sentence to the. htaccess file:

Php_flag MAGIC_QUOTES_GPC Off

3 Masking in code

This method is the most portable, regardless of the server configuration, as long as the support of PHP can be used.

Add the following code at the beginning of all PHP files

if (GET_MAGIC_QUOTES_GPC ()) {function Stripslashes_deep ($value) {$value =is_array ($value)? Array_map (' Stripslashes_ Deep ', $value): Stripslashes ($value); return $value;} $_post=array_map (' Stripslashes_deep ', $_post); $_get=array_map (' Stripslashes_deep ', $_get); $_COOKIE=array_map (' Stripslashes_deep ', $_cookie); $_request=array_map (' Stripslashes_deep ', $_request);}

Articles you may be interested in

    • JS Address bar effect (displays the size of all linked images within the page and the height of the current browser)
    • Analyze the reasons of Baidu Index decreasing and how to improve Baidu index quantity
    • How PHP clears HTML formatting and removes whitespace from text and then intercepts text
    • PHP Get time interval method Summary, PHP display forum posting time interval method Daquan
    • PHP methods for serializing variables in PHP Four types of serialization variables
    • PHP Records search engine Routing and search input keywords
    • Differences in PHP after adding the static keyword to variables and functions
    • Summarize MySQL server query slow cause and solution

http://www.bkjia.com/PHPjc/764083.html www.bkjia.com true http://www.bkjia.com/PHPjc/764083.html techarticle The server space provided by the general Space quotient default PHP directive MAGIC_QUOTES_GPC is on, that is, open. You can then use the stripslashes () function to remove the auto-added backslash. Use a.

  • 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.