Forum Moderators: coopster
I'd like to hear what kind of protection you use for making sure users input valid data into forms, and dont input ' (quote) " (doublequote) etc.
I have tried to do a str_replace, but with little success:
function remove_harmful_code($variable){
$variable = str_replace("'", "'", $variable);
$variable = str_replace('"', """, $variable);
return $variable;
}
I get the same results if using htmlspecialchars, it simply doesnt replace the " and the '....
Any help is appreciated! :-)
addslashes(). Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte).
PHP Manual - addslashes() [php.net]
i.e.
$user_input=addslashes($_POST["user_comment"]); To use
str_replace() to swap quotes for apostrophes you'll need $user_input=str_replace("\"","'",$_POST["user_comment"]); I said, "You love it!" I said, 'You love it!' To use
str_replace() to keep existing quotes you'll need $user_input=str_replace("\"","\\\"",$_POST["user_comment"]); Before:I said, "You love it!"
After:I said, \"You love it!\"
To use
htmlspecialchars() to change any HTML special character into its equivalent HTML syntax you'd do $user_input=htmlspecialchars($_POST["user_comment"]); Before:I said, "You love it!"
After:I said, "You love it!"
To use
htmlentities() to change any HTML entity into its equivalent HTML syntax you'd do $user_input=htmlentities($_POST["user_comment"]); Before:I said, "You love it!"
After (oddly):I said, "You love it!"