PHP Function details: magic_quotes_gpc ()

Source: Internet
Author: User
PHP Function details: magic_quotes_gpc ()

Obtain the value of the PHP environment variable magic_quotes_gpc, which is a function of the PHP System.

Syntax: long get_magic_quotes_gpc (void );

Return value: long integer

What does this function do?

This function gets the magic_quotes_gpc (GPC, Get/Post/Cookie) value of the variable configured in the PHP environment. If 0 is returned, this function is disabled. if 1 is returned, this function is enabled. When magic_quotes_gpc is enabled, all '(single quotation marks),' (double quotation marks), (backslash) and null characters are automatically converted to overflow characters containing the backslash.

In the php configuration file, a Boolean value is set to magic_quotes_runtime. When it is enabled, most php functions automatically add backslashes to overflow characters in external imported (including database or file) data. Of course, if you repeatedly add a backslash to the overflow character, there will be multiple backlash in the string, so you need to use set_magic_quotes_runtime () and get_magic_quotes_runtime () to set and detect php. the magic_quotes_runtime status in the INI file.

In order to make your programs run normally regardless of the server settings. You can use get_magic_quotes_runtime to check the status of the setting and determine whether to manually process it. or, you can use set_magic_quotes_runtime (0) to disable the setting at the beginning (or when automatic escape is not required.

Magic_quotes_gpc: determines whether to automatically add a backslash to '"\ in the data sent by GPC (get, post, cookie. You can use get_magic_quotes_gpc () to detect system settings. If this setting is not enabled, you can use the addslashes () function to add backslashes before certain characters for database query statements. These characters are single quotation marks ('), double quotation marks ("), backslash (\), and NUL (NULL ).

The general usage is as follows:

If (! Get_magic_quotes_gpc () {addslashes ($ prot );}

In the manual, the string addslashes (string str) describes the usage and function of get_magic_quotes_gpc. By default, the PHP command magic_quotes_gpc is on, which automatically runs addslashes () on all GET, POST, and COOKIE data (). Do not use addslashes () for strings that have been escaped by magic_quotes_gpc, because this causes double-layer escape. In this case, you can use the get_magic_quotes_gpc () function for detection.

In fact, this function is used to determine whether PHP automatically calls the addslashes function:

Magic_gpc in the most popular group buying system

  1. Define ('sys _ MAGICGPC ', get_magic_quotes_gpc ());

  2. $ _ POST = magic_gpc ($ _ POST );

  3. Function magic_gpc ($ string ){
  4. If (SYS_MAGICGPC ){
  5. If (is_array ($ string )){
  6. Foreach ($ string as $ key => $ val ){
  7. $ String [$ key] = magic_gpc ($ val );
  8. }
  9. } Else {
  10. $ String = stripslashes ($ string );
  11. }
  12. }
  13. Return $ string;
  14. }

  15. Echo 'get _ magic_quotes_gpc value: '. get_magic_quotes_gpc ();
  16. Echo'
    ';
  17. Echo 'directly output the POST variable: '. $ _ POST ['nowamagic'];
  18. Echo'
    ';
  19. Echo 'processed by magic_gpc:'. magic_gpc ($ _ POST ['nowamagic ']);
  20. ?>







Program output:

Value of get_magic_quotes_gpc: 1 The POST variable: no 'wamagic. net is directly output by magic_gpc: no 'wamagic. netAnother example:


  1. Echo 'get _ magic_quotes_gpc: '. get_magic_quotes_gpc ();
  2. Echo'
    ';
  3. Echo 'directly output the 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 'output after processing: '. $ nowamagic;
  14. ?>







Program output:

Get_magic_quotes_gpc: 1 directly outputs the 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.