[英]Special characters with in php - javascript - ajax
我有简单的注释脚本,可以添加/编辑/删除注释。 我对特殊字符有疑问。
如果var currentMessage包含'或',则代码中断。
我将注释保存在数据库中,如下所示:
$ Message = clean_data($ _ POST [“ txtmessage”]); 然后做一个典型的mysqli_query保存....
function clean_data($input) {
$input = trim(htmlentities(strip_tags($input,",")));
if (get_magic_quotes_gpc())
$input = stripslashes($input);
$input = stripslashes($input);
$input = htmlspecialchars($input, ENT_IGNORE, 'utf-8');
$input = trim($input);
$sqllink = new mysqli(MySQL_Host, MySQL_UserName, MySQL_Password, MySQL_Database);
$input = mysqli_real_escape_string($sqllink, $input);
no_magic_quotes($input);
$input = str_replace("'", "'", $input);
$input = str_replace("&", "&", $input);
$input = str_replace('"', """, $input);
$input = strip_tags($input);
return $input;
}
保存在MySQL中的数据如下: let's do it !
我的编辑按钮是这样的:
<button id="BtnEdit_<?php echo $r_c['CommentID']; ?>" class="btn btn-xs btn-warning" onClick="showEditBox(this,<?php echo $r_c['CommentID']; ?>)" name="edit"><i class="fas fa-edit"></i> <? echo _("To Edit"); ?></button>
我的消息显示如下:
<div class="message-content"><? echo $r_c['Message']; ?></div>
如果我在php echo中显示特殊字符,则javascript无法正常工作。 如果我删除所有的'或“”就可以了。 但这是不实际的。
function showEditBox(editobj,id) {
$('#BtnEdit_'+ id).hide();
$('#frmAdd').show();
var currentMessage = $("#message_" + id + " .message-content").html();
var editMarkUp = '<input type="text" class="form-control input-sm" name="txtmessage" id="txtmessage_'+id+'" value="'+currentMessage+'"><br><div class="btn-group pull-left"><button class="btn btn-xs btn-success" name="ok" onClick="callCrudAction(\'edit\','+id+')"><i class="fas fa-check"></i> <? echo _("Save"); ?></button> <button class="btn btn-xs btn-default" name="cancel" onClick="cancelEdit(\''+currentMessage+'\','+id+')"><i class="fas fa-times"></i> <? echo _("Cancel"); ?></button></div>';
$("#message_" + id + " .message-content").html(editMarkUp);
}
数据显示如下:开始吧! (如果这样会更好-让我们开始吧!)
如果注释中有',则编辑按钮甚至不起作用。
如果使用准备好的语句,则不必破坏数据。 即便如此,您仍然堆积了许多相互冲突的数据损坏层-哎呀,我的意思是“消毒” -如果这是您遇到的第一个或唯一问题,我会感到惊讶。
// Validate that the data is the type/size you expect, but don't modify it
$message = basicValidation($_POST["txtmessage"]);
$stmt = $mysqli->prepare('INSERT INTO foo VALUES (?)')
$stmt->bind_param("s", $message);
$stmt->execute();
这可以保护您免受SQL注入的侵扰,并确保完全按照指定的顺序插入数据,而不必对HTML实体,反斜杠或其他仅应应用于输出的事情进行任何废话。
检索数据以进行显示是您应该应用转换以使其安全地包含在HTML或任何其他可能使用的格式中的时候。 您不想将一堆废话HTML实体注入PDF文档中,对吗?
$message = mysqli_nonsense();
?><div>
<p><?= htmlentities($message) ?></p>
</div>
这可以保护您免受JavaScript注入。
这么简单,不是吗?
通过调用htmlentities
, strip_tags
, stripslashes
, htmlspecialchars
, mysqli_real_escape_string
或no_magic_quotes
,您将不需要所有的清理no_magic_quotes
。 尽管您没有在代码中插入数据,但它表明您没有使用准备好的语句来插入数据。 这正是您应该追求的。
它可能看起来像这样:
用PHP编写数据库:
$message = $_POST["txtmessage"];
$sqllink = new mysqli(MySQL_Host, MySQL_UserName, MySQL_Password, MySQL_Database);
$stmt = $sqllink->prepare("INSERT INTO messages(message) VALUES(?)");
$stmt->bind_param("s", $message);
$stmt->execute();
$stmt->close();
HTML生成应确保必要时使用HTML实体,尤其是用于&符。 因此,请在此处调用htmlspecialchars
:
<div class="message-content"><? echo htmlspecialchars($r_c['Message']); ?></div>
JavaScript按钮单击处理程序不应从消息中获取html
,而应获取text
。 另外,不要在HTML代码段中设置input
的value
; 而是用val()
赋值:
function showEditBox(editobj, id) {
$('#BtnEdit_'+ id).hide();
$('#frmAdd').show();
// Use text():
var currentMessage = $("#message_" + id + " .message-content").text();
// Instead of building HTML with string concatenation, build nodes with jQuery.
// That way you don't have the problem of escaping/encoding
var nodes = [
$("<input>").addClass("form-control input-sm").attr({
type: "text", name: "txtmessage", id: "txtmessage_"+id
}).val(currentMessage), // <-- here is where the value is set
$("<br>"),
$("<div>").addClass("btn-group pull-left").append(
$("<button>").addClass("btn btn-xs btn-success").attr({ name: "ok" }).append(
$("<i>").addClass("fas fa-check"),
"<? echo _("Save"); ?>"
).click(callCrudAction.bind(null, 'edit', id))
),
"\xA0\xA0\xA0\xA0", // non-breaking spaces
$("<button>").addClass("btn btn-xs btn-default").attr({ name: "cancel" }).append(
$("<i>").addClass("fas fa-times"),
"<? echo _("Cancel"); ?>"
).click(cancelEdit.bind(null, currentMessage, id))
];
$("#message_" + id + " .message-content").empty().append(nodes);
}
我没有更改HTML,但是应该使用CSS为按钮添加边距,而不是产生不间断的空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.