PHP Basics (i)-Magic references

Source: Internet
Author: User
Tags php basics
PHP Basics (1)-Magic references
PHP Basics-Magic References

Sample code download page http://xieye.iteye.com/blog/1336095 (in attachment)

Magic reference refers to the server's automatic conversion of some characters to facilitate the programmer's approach.

Source: In the beginning, the PHP designers to facilitate the programmer to add form data to the database, in the configuration file added a setting, when the selected item is opened, the Magic Reference is enabled, when the selected item is closed, the Magic reference is not enabled.

PHP.ini about 441 Lines
MAGIC_QUOTES_GPC = Off
Note Yes; Magic quotes for incoming Get/post/cookie data.
Note that the Get/post/cookie data that enters the server is automatically converted when on.

1) addslashes function
Before introducing the Magic reference, first introduce the Addslashes function

The function returns a string that is preceded by a backslash in order for the database query statement to be preceded by some characters. These characters are single quotes ('), double quotation marks ("), backslashes (\), and NUL (the NULL character).

An example of using addslashes () is when you want to enter data into the database. For example, the name O ' Reilly is inserted into the database, which needs to be escaped. Most databases use \ as escape character: O\ ' Reilly. This allows the data to be placed in the database without inserting additional \. When PHP instruction Magic_quotes_sybase is set to ON, it means that the insert ' will be used ' to escape.

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_MAGIC_QUOTES_GPC () to detect this situation.

Example 1. Addslashes () example
http://localhost/command/peixun/magicquote/1.php
!--? PHP$STR = "Is your name O ' Reilly?"; /output: Is your name o\ ' Reilly?echo addslashes ($STR);  

2) test configuration options MAGIC_QUOTES_GPC
First ensure that MAGIC_QUOTES_GPC = on, if it is off or 0, you need to modify and restart the Apache server.
http://localhost/command/peixun/magicquote/2.php
!--? phprequire_once (' Public/commandline.php '); $isget = ($_server[' request_method '] = = ' GET ')?    1:0;if ($isget) {$html = <<
   
     longs;    Echo $html;    } else {echo $_post[' name1 ']; 
   

Then Modify MAGIC_QUOTES_GPC = Off, restart the Apache server, and test the program again. The final explanation for

is that these are all learning concepts, and in most of the actual applications, the magic references are turned off and converted manually.




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