PHP Functions in detail: MAGIC_QUOTES_GPC ()

Source: Internet
Author: User
Gets the PHP environment variable MAGIC_QUOTES_GPC value, belongs to the PHP system function. Syntax: Long get_magic_quotes_gpc (void); return value: Length integerwhat does this function do? This function obtains the PHP environment configuration variable MAGIC_QUOTES_GPC (GPC, Get/post/cookie) value. Returns 0 to turn off this function; return 1 indicates that this function is turned on. When MAGIC_QUOTES_GPC is turned on, all ' (single quotes), "(double quotes), (backslash) and null characters are automatically converted to overflow characters that contain backslashes. In the PHP configuration file, there is a Boolean setting, which is magic_quotes_runtime. When it is opened, most of PHP's functions automatically add backslashes to the overflow characters in the data (including database or file) that are introduced externally. Of course, if you repeat the multibyte backslash for the overflow character, there will be multiple backslashes in the string, so use Set_magic_quotes_runtime () and Get_magic_quotes_runtime (). Sets and detects the Magic_quotes_runtime status in the php.ini file. In order to make your own program regardless of the server is what settings can be performed normally. You can use Get_magic_quotes_runtime to detect the state of the setting at the beginning of the program to determine whether you want to handle it manually, or to turn off the setting with Set_magic_quotes_runtime (0) when you start (or do not need automatic escaping). MAGIC_QUOTES_GPC sets whether the ' "\ Plus backslashes in the data coming from GPC (Get,post,cookie) are automatically set. System settings can be detected with GET_MAGIC_QUOTES_GPC (). If you do not open this setting, you can use the Addslashes () function to add, which is the function of the database query statements and so on need to precede some characters with a backslash. These characters are single quotes ('), double quotation marks ("), backslashes (\), and NUL (the NULL character). The general usage is as follows: if (!GET_MAGIC_QUOTES_GPC ()) {addslashes ($prot);} In the manual, when string addslashes (String str) is introduced, there is a sentence explaining the use and role of GET_MAGIC_QUOTES_GPC. By default, PHP instruction MAGIC_QUOTES_GPC is on, and it is primarily for all GET, POST, and COOKIE data automatically run Addslashes (). Do not use Addslashes () for strings that have been MAGIC_QUOTES_GPC escaped, because this results in double-layer escaping. You can use the function get_ when you encounter this situationMAGIC_QUOTES_GPC () for testing. In fact, this function is to determine whether PHP automatically calls addslashes this function: MAGIC_GPC in the most Earth buying system
    1. !--? php
    2. define (' SYS_MAGICGPC ', GET_MAGIC_QUOTES_GPC ());

    3. $_post = MAGIC_GPC ($_post);

    4. function magic_gpc ($string) {
    5. if (SYS_MAGICGPC) {
    6. if (Is_array ($string)) {
    7. foreach ($string as $key = $val) {
    8. $string [$key] = MAGIC_GPC ($val);
    9. }
    10. } else {
    11. $string = stripslashes ($string);
    12. }
    13. }
    14. return $string;
    15. }

    16. echo ' GET_MAGIC_QUOTES_GPC value: '. GET_MAGIC_QUOTES_GPC ();
    17. echo '
      ';
    18. Echo ' directly outputs the POST variable: '. $_post[' nowamagic '];
    19. echo '
      ';
    20. Echo ' is processed by MAGIC_GPC: '. MAGIC_GPC ($_post[' nowamagic ');
    21. ?






Copy Code
Program output: GET_MAGIC_QUOTES_GPC value: 1 Direct output post variable: No ' wamagic.net processed by MAGIC_GPC: No ' wamagic.netOne more example:

    1. Echo ' GET_MAGIC_QUOTES_GPC: '. GET_MAGIC_QUOTES_GPC ();
    2. Echo '
      ';
    3. echo ' Direct output POST variable: '. $_post[' nowamagic '];
    4. Echo '
      ';
    5. Echo ' addslashes: '. Addslashes ($_post[' nowamagic ');

    6. if (!GET_MAGIC_QUOTES_GPC ()) {
    7. $nowamagic = addslashes ($_post[' nowamagic ');
    8. }
    9. else {
    10. $nowamagic = $_post[' nowamagic ');
    11. }

    12. Echo '
      ';
    13. echo ' Processed output: '. $nowamagic;
    14. ?>






Copy Code
Program output: Get_magic_quotes_gpc:1 Direct output post variable: No ' wa\magic.netaddslashes:no\ ' wa\\magic.net after processing output: No ' wa\magic.net
Basic knowledge
  • 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.