Examples of differences between PHP stripslashes () functions and addslashes () functions

Source: Internet
Author: User
The function of the addslashes () function and the stripslashes () function in PHP and some usage are described earlier in this article to introduce PHP stripslashes () function and addslashes () functionThe difference.

they differ from the function name : Strip (Peel) slashes (slash) Add (add) slashes (slash), so one of them is a stripped slash and the other is to add a slash action is the opposite.

usage Scenarios : In fact, these two functions are mainly used in some need to convert special characters in the data processing, such as database operations, when writing to the database, we need to put a single quotation mark such a special character escaped after the save, while reading,

We also need to reverse the escape of these special characters in the database back.

When we write data to MySQL, for example:

mysql_query ("Update table set ' title ' = ' Kuhanzhu ' s blog ')";

That's going to go wrong. As with ASP, the database will be allergic to single quotes. And addslashes at this time the longest face, with the ASP's replace ("'", "" "," Kuhanzhu's Blog ") function the same.

PHP for security, so introduced a MAGIC_QUOTES_GPC = on function, you can directly put single quotation marks into the database without any processing, then for off, you need to consider the problem of single quotes, rather than blindly trust the operating environment.

When MAGIC_QUOTES_GPC = ON, using the addslashes () processed data in the database will be in the form of \ ', if the direct output at this time, you will find a more than the content of their expectations, so stripslashes (), it can put \ Remove (differs from str_replace ("\", "", $Str)).

When the MAGIC_QUOTES_GPC = off, the use of addslashes () processed data in the database will be ' form ' to save, there is no problem mentioned above, addslashes () play the role of inserting data without error, if the direct output at this time, the data is normal. No need to use stripslashes ().

Addslashes () and stripslashes () are exactly the opposite, Direct Memory: addslashes () Add a \,stripslashes () to a \

So when do you use it?

Simply put:

When MAGIC_QUOTES_GPC = ON, the system automatically handles issues such as single quotes, with no addslashes () and stripslashes (), but if the data is added with Addslashes (), Then the data must be displayed stripslashes ()

When MAGIC_QUOTES_GPC = off, the system does not handle problems such as single quotes, so you must use Addslashes () when inserting data, and you do not need to use stripslashes () when displaying data.

Since there is analysis, what to do when the program? Depending on the above two conditions, it is possible to:

Regardless of whether MAGIC_QUOTES_GPC is on or off, we use addslashes () when we add data, and when on, we must use Stripslashes (), which is not stripslashes () when off.

How do I tell if it is on or off? Use GET_MAGIC_QUOTES_GPC (). see this article for the GET_MAGIC_QUOTES_GPC () function

Instance

The code is as follows:

Code//Submit data, or variable preparation: $Content =addslashes ("This is data, whether there is no single quotation mark or variable);//Insert data into the database, the code omits//begins to display 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.