这是我遇到的问题,例如,当用户输入 <script>top.location.href=’http://www.google.nl’;</script>
时
我希望我的应用程序将其作为纯文本进行回显。现在,这实际上适用于
htmlspecialchars()
这个例子对我有用:
$test = "<script>top.location.href=’http://www.google.nl’;</script>";
echo htmlspecialchars($test);
但是,当用户提交表单时,数据会转到我的数据库,然后返回到“仪表板”。
现在的值是 ''
。
有没有办法将数据安全地保存到我的数据库中?
我通过 SDK 以这种方式将值添加到我的 C# 应用程序的数据库中:
$onderwerp = htmlspecialchars(stripslashes(trim($_POST['onderwerp'])), ENT_QUOTES,'UTF-8',true);
$omschrijving = htmlspecialchars(stripslashes(trim($_POST['omschrijving'])), ENT_QUOTES,'UTF-8',true);
$im = array('description' => mysql_real_escape_string($onderwerp),
'message' => mysql_real_escape_string($omschrijving) ,
'relation' => $_SESSION['username'],
'messageType' => 70,
'documentName' => $_FILES["file"]["name"],
'documentData' => base64_encode(file_get_contents($_FILES["file"]["tmp_name"])));
$imresponse = $wcfclient->CreateInboundMessage($im);
echo $imresponse->CreateInboundMessageResult;
然后在我的仪表板上以这种方式调用它们:
$roc = array('relation' => $_SESSION['username']);
$rocresponse = $wcfclient->ReadOpenCalls($roc);
foreach ($rocresponse->ReadOpenCallsResult as $key => $calls){
echo $calls->Description;
}
最佳答案
mysql_real_escape_string():
mysql_real_escape_string() 函数转义字符串中的特殊字符以在 SQL 语句中使用
同时检查SQL注入(inject):SQL Injection
示例
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$item = "Zak's and Derick's Laptop";
$escaped_item = mysql_real_escape_string($item);
printf ("Escaped string: %s\n", $escaped_item);
?>
输出:
Escaped string: Zak\'s and Derick\'s Laptop
关于javascript - 将 JavaScript 值以明文形式保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18823337/