Trying to stop bullets in textareas from entering my database as special characters

一曲冷凌霜 提交于 2019-12-10 10:36:57

问题


I am using this currently, but it doesn't seem to be working for bullets:

function sanitizeMySQL($var){
        $var = mysql_real_escape_string($var);
        $var = sanitizeString($var);
        return $var;
}

function sanitizeString($var)
{
    $var = str_replace('•','•', $var);
    $var = htmlentities($var);
    $var = strip_tags($var);
    return $var;
}

This is what bullets show up like in my db after someone has submitted them through a textarea:

•

EDIT: This is now what I am getting: •.

I do have bullets stored in my db, so I know it allows them. Is there a correct way to store bullets in latin-1 encoding?


回答1:


The data that is submitted through your form and your source code do not have the same encoding. Therefore the • characters from your source code do not match the ones in the actual data. Therefore they are not being replaced. Unify on a common encoding. See Handling Unicode Front To Back In A Web App.

Also, your sanitization strategy is pretty weird. I don't know what you have against "•", this should not be replaced in a general "sanitization" function. Furthermore, you're first HTML escaping everything, then are stripping tags. Hint: there won't be any tags anymore after you have escaped them. Next, you should not modify the string anymore after you have SQL escaped it. See The Great Escapism (Or: What You Need To Know To Work With Text Within Text).



来源:https://stackoverflow.com/questions/11674531/trying-to-stop-bullets-in-textareas-from-entering-my-database-as-special-charact

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!