Differences between phpstripslashes and addslashes _ PHP Tutorial

Source: Internet
Author: User
Differences between phpstripslashes and addslashes. This article mainly introduces the differences between phpstripslashes and addslashes. When we write data to mysql, for example, the code is as follows: mysql_query (update this article mainly introduces the differences between php stripslashes and addslashes, which is convenient for users who need it.

When we write data to mysql, for example, the 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. For security purposes, PHP introduces the magic_quotes_gpc = On function. you can directly insert single quotes into the database without any processing. for Off, you need to consider the single quotes, rather than blindly trusting the runtime environment. When magic_quotes_gpc = On, data processed using addslashes () will be saved as \ in the database. if the data is output directly at this time, the stripslashes () is displayed, and it can remove \ (different from str_replace ("\", "", $ Str )). When magic_quotes_gpc = Off, the data processed using addslashes () will be saved in the database in the form of ', without the \ problem mentioned above, addslashes () it does not cause data insertion errors. if data is output directly at this time, the data is normal. You do not need to use stripslashes (). Addslashes () and stripslashes () are the opposite. direct memory: addslashes () is added with \, and stripslashes () is used? To put it simply: when magic_quotes_gpc = On, the system automatically handles single quotes and other issues. it does not matter if addslashes () and stripslashes () are used, but if addslashes () is used to add data (), when displaying data, you must use stripslashes (). when magic_quotes_gpc = Off, the system will not handle single quotation marks and other issues. Therefore, you must use addslashes () to insert data (), you do not need to use stripslashes () when displaying data (). What should I do when I make a program? Based On the above two cases, you can obtain: whether magic_quotes_gpc is On or Off, we use addslashes () when adding data. when On, we must use stripslashes (), when Off, stripslashes () cannot be used (). How can we determine whether to be On or Off? Use get_magic_quotes_gpc (). Example: the code is as follows: code // submit data, or variable preparation: $ Content = addslashes ("This is data, no matter whether there are single quotes or variables "); // Insert data to the database. the code is omitted. // start to display the data $ Content = "data read from the database"; if (get_magic_quotes_gpc ()) {$ Content = stripslashes ($ Content);} echo $ Content;

Differences between stripslashes and addslashes. 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.