Introduction to the difference between PHP stripslashes and addslashes

Source: Internet
Author: User

  This article mainly introduces the difference between PHP stripslashes and addslashes, sharing the convenience needs of friends.

When we write data to MySQL, for example: The code is as follows: mysql_query ("Update table set ' title ' = ' Kuhanzhu ' blog");     That's going to go wrong. As with ASP, the database is allergic to single quotes. And addslashes at this time the longest face, and ASP's replace ("'", "" "," Kuhanzhu ' s Blog ") function the same.     PHP for security, so introduced a MAGIC_QUOTES_GPC = on function, you can not do any processing can directly insert single quotes into the database, then for off, you need to consider the issue of single quotes, rather than blindly trust the running environment.   When MAGIC_QUOTES_GPC = on, using the addslashes () processed data in the database will be ' form, if the direct output, you will find more than you expect the content of a more, so Stripslashes () appeared, It can be removed (distinguished from str_replace ("", "", $Str)).   When MAGIC_QUOTES_GPC = off, the use of addslashes () processed data in the database will be ' form to save, without the above mentioned problems, Addslashes () play the role of inserting data 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, stripslashes () to a   then when to use it?   simply:   When MAGIC_QUOTES_GPC = ON, the system automatically handles issues such as single quotes, with no addslashes () and stripslashes (), but it doesn't matter if you add the data with Addslashes () , then the data must be displayed stripslashes ()   When MAGIC_QUOTES_GPC = off, the system does not process single quotes and so on, so inserting data must use Addslashes (), You do not need to use stripslashes () when displaying data.   Since there is analysis, what to do when doing the program? According to both of the above, we can get:   Whether MAGIC_QUOTES_GPC is on or off, I addThe data is added with Addslashes (), and when on, the stripslashes () must be used, and the stripslashes () cannot be used when off.   How do you judge on or off? Use GET_MAGIC_QUOTES_GPC ().   Last example:   code is as follows: Code  //submit data, or variable preparation: $Content =addslashes ("This is the data, whether there are no single quotes or variables"); Inserts data into the database, the code omits//starts displaying the data $Content = "Data read from the database"; if (GET_MAGIC_QUOTES_GPC ()) {  $Content =stripslashes ($Content); } echo $Content;  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.