PHP stripslashes and addslashes Differences _php tutorials

Source: Internet
Author: User
When we write data to MySQL, for example:

Copy the Code code as follows:
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, the data processed using addslashes () is saved in the database in the form of \ ', and if it is directly output at this time, it will be found to be more than what you expect, so Stripslashes () , it can remove the \ (different from str_replace ("\", "", $Str)).

When MAGIC_QUOTES_GPC = Off , the data processed using addslashes () will be saved in the database as ' form ', without the problem of the above mentioned \ Addslashes () The effect of inserting data is not wrong, if output directly 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 ().

The last example:

Copy the Code code as follows:
Code
Submit data, or variable preparation:
$Content =addslashes ("This is data, whether there is a single quote or a variable");
Inserting data into the database, omitting the code
Start displaying data
$Content = "Data read from the database";
if (GET_MAGIC_QUOTES_GPC ()) {
$Content =stripslashes ($Content);
}
Echo $Content;

http://www.bkjia.com/PHPjc/728080.html www.bkjia.com true http://www.bkjia.com/PHPjc/728080.html techarticle when we write data to MySQL, for example: Copy code code as follows: mysql_query ("Update table set ' title ' = ' Kuhanzhu ' s blog '); that's going to go wrong. As with ASP, the database will ...

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