The difference between PHP stripslashes and addslashes _php tutorial

Source: Internet
Author: User
This article mainly introduces the differences between PHP stripslashes and addslashes, sharing the convenience of friends.

When we write data to MySQL, for example: The code is 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, 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 just the opposite, Direct Memory: addslashes () plus a \,stripslashes () go to a \ So when to use it? Simply put: when MAGIC_QUOTES_GPC = ON, the system automatically handles issues such as single quotes, which doesn't matter with addslashes () and stripslashes (), but if you add data with addslashes (), Then the data must be displayed stripslashes () when MAGIC_QUOTES_GPC = off, the system does not handle such problems as single quotes, so the data must be inserted using addslashes (), the data is displayed without using Stripslashes (). Since there is analysis, what to do when the program? According to the above two cases, you can get: regardless of whether the MAGIC_QUOTES_GPC is on or off, we add data with addslashes (), when on, must use Stripslashes (), off is not used stripslashes (). How do I tell if it is on or off? With Get_mAGIC_QUOTES_GPC (). The final example: Code: 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 the data $content=" Data read from the database "; if (GET_MAGIC_QUOTES_GPC ()) {$Content =stripslashes ($Content);} Echo $Content;

http://www.bkjia.com/PHPjc/730219.html www.bkjia.com true http://www.bkjia.com/PHPjc/730219.html techarticle This article mainly introduces the differences between PHP stripslashes and addslashes, sharing the convenience of friends. When we write data to MySQL, for example: The code is as follows: mysql_query (update ...

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