Differences between php stripslashes and addslashes

Source: Internet
Author: User

When we write data to mysql, for example:

Copy codeThe Code is as follows:
Mysql_query ("update table set 'title' = 'kuhanzhu's blog '");


Then an error occurs. Like asp, databases are allergic to single quotes. Addslashes has the longest face at this time, just like asp's replace ("'", "," kuhanzhu's blog ") function.

PHP introducesMagic_quotes_gpc = OnYou can directly insert single quotes into the database without any processing. When Off, you need to consider the single quotes, rather than blindly trusting the runtime environment.

WhenMagic_quotes_gpc = OnTheAddslashes ()The processed data will be saved as \ 'in the database. If the processed data is output directly at this time, it will find that there are more \ than the expected content. ThereforeStripslashes ()It can remove \ (different from str_replace ("\", "", $ Str )).

WhenMagic_quotes_gpc = OffTheAddslashes ()The processed data will be saved in the database in the form of '. Without the \ problem mentioned above, addslashes () plays the role of no error in data insertion. If the data is output directly at this time, the data is normal. No need to useStripslashes ().

Addslashes () and stripslashes () are the opposite. Direct Memory: addslashes () add \, stripslashes () \

So when will it be used?

To put it simply:

When magic_quotes_gpc = On, the system automatically handles single quotes and other issues. It does not need to use addslashes () or stripslashes (), but if addslashes () is used to add data (), stripslashes () is required for data display ()

When magic_quotes_gpc = Off, the system will not handle single quotation marks and other issues. Therefore, addslashes () must be used when inserting data, and stripslashes () is not required when displaying data ().

What should I do when I make a program? Based on the above two situations, you can:

Whether magic_quotes_gpc is On or Off, we use addslashes () when adding data. When On, we must use stripslashes (), and when Off, we cannot use stripslashes ().

How can we determine whether to be On or Off? Use get_magic_quotes_gpc ().

Example:

Copy codeThe Code is as follows:
Code
// Submit data or prepare variables:
$ Content = addslashes ("This is data, no matter whether there are single quotes or variables ");
// Insert data to the database. The code is omitted.
// Start displaying data
$ Content = "data read from the Database ";
If (get_magic_quotes_gpc ()){
$ Content = stripslashes ($ Content );
}
Echo $ Content;

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.