javascript - 将 JavaScript 值以明文形式保存在数据库中

标签 javascript php html mysql

这是我遇到的问题,例如,当用户输入 <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():

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/

相关文章:

javascript - 仅处理具有特定类别的第一张图像

php - 如何从 codeigniter 中的数据库表中获取特定值

javascript - 使用循环将数组数据从 php 传递到 javascript

php - 交响乐 2 : Using Twig extension in form builder

javascript - 在 elevateZoom 中禁用 Hover for Mobile 设备的缩放效果

javascript - Fullpage.js 部分内的可滚动 div 不起作用

javascript - 使用通用 javascript 时无法访问 Vue 组件中的 DOM 元素

javascript - html 表单无法识别输入

javascript - 在 Firefox 中启用自定义元素

javascript - 如果元素包含任何这些单词,则将该单词包装在 span 中