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;