【发布时间】:2010-10-10 10:07:48
【问题描述】:
有时当用户将数据复制并粘贴到输入表单中时,我们会得到如下字符:
ddn’t,” 用于开头引号和 †用于结尾引号等...
我使用此例程来清理 Web 表单上的大多数输入(我之前写过,但也在寻找改进):
function fnSanitizePost($data) //escapes,strips and trims all members of the post array
{
if(is_array($data))
{
$areturn = array();
foreach($data as $skey=>$svalue)
{
$areturn[$skey] = fnSanitizePost($svalue);
}
return $areturn;
}
else
{
if(!is_numeric($data))
{
//with magic quotes on, the input gets escaped twice, which means that we have to strip those slashes. leaving data in your database with slashes in them, is a bad idea
if(get_magic_quotes_gpc()) //gets current configuration setting of magic quotes
{
$data = stripslahes($data);
}
$data = pg_escape_string($data); //escapes a string for insertion into the database
$data = strip_tags($data); //strips HTML and PHP tags from a string
}
$data = trim($data); //trims whitespace from beginning and end of a string
return $data;
}
}
我真的很想避免上面提到的字符被存储在数据库中,我是否需要在我的清理程序中添加一些正则表达式替换?
谢谢,
-尼古拉斯
【问题讨论】:
-
这是一个智能引用问题,显然这是一个似乎只有在 MS Word 参与剪切/粘贴时才会出现的问题。如果我想在进入数据库之前替换这些,我应该创建一个字符数组还是它们的代码? (——或者它的数值?)
标签: php string sanitization