How to Use PHP daddslashes

Source: Internet
Author: User

Instance code: Copy codeThe Code is as follows: @ set_magic_quotes_runtime (0 );
$ MQG = get_magic_quotes_gpc ();

If (! $ MQG & $ _ POST) $ _ POST = daddslashes ($ _ POST );
If (! $ MQG & $ _ GET) $ _ GET = daddslashes ($ _ GET );

// Translated character Functions
Function daddslashes ($ string ){
If (! Is_array ($ string) return addslashes ($ string );
Foreach ($ string as $ key => $ val) $ string [$ key] = daddslashes ($ val );
Return $ string;
}

PHP provides two magic reference functions for reference data: magic_quotes_gpc and magic_quotes_runtime. when the ini is set to ON, the single quotation marks and double quotation marks will be applied to the referenced data, and the backslash will be automatically added to the backslash \ To help us automatically translate the symbols, make sure that the data operation runs correctly. However, in different php versions or server configurations, some magic_quotes_gpc and magic_quotes_runtime are set to on, while others are off, therefore, the program we write must meet the on and off conditions. So what are the differences between magic_quotes_gpc and magic_quotes_runtime functions? See the following description:

Magic_quotes_gpc and magic_quotes_runtime Functions

Magic_quotes_gpc
Scope of application: web customers
Server;
Time: The request starts, for example, when the script is running.

Magic_quotes_runtime

Scope of application: data read from files or obtained from SQL queries;
Time: each time a script accesses the data generated in the running state.

Therefore, the value of magic_quotes_gpc will affect the value of magic_quotes_runtime for the data obtained through Get/Post/Cookies. It will affect the data read from the file or the data queried from the database.

Example:Copy codeThe Code is as follows: $ data1 =$ _ POST ['aaa'];
$ Data2 = implode(file('1.txt '));

If (get_magic_quotes_gpc ()){
// Write data $ data1 directly to the database (automatic translation)
} Else {
$ Data1 = addslashes ($ data1 );
// Write data $ data1 to the database and translate it using a function (addslashes)
}

If (get_magic_quotes_runtime ()){
// Write data $ data2 directly to the database (automatic translation)
// The stripslashes () output after a stripslashes () read from the database is to remove: \, which is opposite to addslashes ().
} Else {
$ Data2 = addslashes ($ data2 );
// Write data $ data2 to the database
// Directly output the data read from the database
}

The most important difference is the two points mentioned above: they are different in processing objects.
The value of magic_quotes_gpc affects the data obtained through Get/Post/Cookies.
The value of magic_quotes_runtime will affect the data read from the file or the data queried from the database.

Here are a few functions that you want to associate:
Set_magic_quotes_runtime ():
Set magic_quotes_runtime value. 0 = close. 1 = open. The default status is disabled.

Get_magic_quotes_gpc ():
View magic_quotes_gpc value. 0 = close. 1 = open

Get_magic_quotes_runtime ():
View the value of magic_quotes_runtime. 0 = close. 1 = open.

Note that the value of magic_quotes_gpc () cannot be set in the program.

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.