Hi Diode
The purpose for cleanQuery is to protect your data from malicious SQL injection attacks. Those attacks only occur when querying a database. The point is that cleanQuery doesn't strip any code out when you use it for other data fields and using it there will not protect you from an SQL injection attack.
Other data needs to be protected from cross-site scripting, but that is a different issue. Cross site scripting typically includes JavaScript in the user-input data, which can reveal critical data to an attacker. A lot of people get the two attacks confused, but they are very different.
You should add backslashes to escape ALL single and double quotes before they are stored in a table. If you are not actively doing that, then magic quotes is turned on and it is doing it for you.
http://www.php.net/addslashes
Yes, you have to remove the slashes when you display the data. You use stripslashes to do that. Read about it on the official PHP page.
http://www.php.net/manual/en/function.stripslashes.php
TopDogger, are you saying I should use your function, because the one I am using (that I found on the web) strips the slashes, but yours adds them.
Both routines add a backslash when a backslash is needed. Stripslashes is being used in cleanQuery simply to avoid duplicate backslashes. It first strips them out, then adds them back in. If it didn't do that, it would add a second backslash.
Example: \" would become \\" because another backslash would be added before the quote.
The escapeString function doesn't add backslashes unless magic quotes is turned off. If magic quotes is turned on, the backslashes are being added and managed by magic quotes.
The functions are very similar. cleanQuery just escapes a few more characters that could be used in an attack.
You may be running into problems with your HTML formatting because cleanQuery is escaping newline (\n) and carriage return (\r) characters that do not need to be escaped for data that is not used as part of a SELECT query. If you make sure that those characters are converted to <br /> code before you use cleanQuery, it probably will not make any difference whether you use one function or the other. cleanQuery has a specific purpose (preventing MySQL attacks with queries) and you are using it for something other than that purpose, which could be the root of your formatting problem.
If you are not already doing so, put all of your functions like these into one PHP file called functions.php and include that file in the top of each of your PHP pages. That way, you never need to duplicate the functions and you just call whichever function is most appropriate for your needs.
BTW, both if (get_magic_quotes_runtime()) and if(get_magic_quotes_gpc()) do the same thing. They just tell you if magic quotes is active.
Does that bring it into perspective? Hope this makes things a little clearer.