【发布时间】:2015-10-04 22:19:22
【问题描述】:
我一直在开发一个社交网络。我注意到一些安全问题,用户可以将 javascript/jquery 中的变量更改为其他 user_id、文本内容和其他已加载到脚本中的信息。这一切都是通过检查工具或其他可以编辑语言的软件完成的。他们甚至可以重写函数。
在将 url_id 发送到 php 函数后,我通过 php 和 sql 将数据加载到页面上。
我有 javascript 和 jquery 脚本作为回报,它们使用这些数据来执行 ajax、post 和 get 请求以及执行功能。
如何阻止用户在将这些变量发送到服务器之前对其进行更改?例如,当用户发布帖子时,他们可以更改 id 以使其成为其他人的帖子,或者当他们单击删除图像时,他们可以删除其他人的图像,这会变得更加复杂。这是一个大问题。
这些脚本包含在 php 页面或通过 ajax 加载的 php 脚本中。
我怎样才能阻止这种情况?你能给我一个简单的解释吗?几个月来我一直在寻找如何阻止这种情况。我仍然不明白如何阻止用户这样做。如果有另一种方法可以做到这一点?你能给我提供100%真实的例子吗?我还有哪些其他选择?
这是我的代码的一些 sn-ps
<? if (login_check($mysqli) == true) : ?>
<script>
$.post("auto/online.php?q=<? echo $id ?>");
function o() {
setTimeout(function() {
$.post("auto/online.php?q=<? echo $id ?>");
o();
}, 6e4);
}
</script>
<? endif; ?>
<?php echo '<div class="post-btn" onclick="ajaxPost(postenter.value,\''.$name.'\',\''.$id.'\');" title="Post">Post</div>'; ?>
function ajaxPost(content,name,id) {
var ip = '<?php echo $ip ?>';
content = content.replace(/<br\s*\/?>/mg,"\n");
var postArray = [content, id, ip];
postArray = JSON.stringify(postArray);
alert(postArray);
if (content.length == 0) {
alert('Oops it looks like your post is empty.');
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("postenter").innerHTML = "";
var html = xmlhttp.responseText;
alert(html);
$(html).hide().insertAfter("#wrapper").fadeIn(500);
document.getElementById("postenter").value = "";
}
}
xmlhttp.open("POST", "auto/post.php", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send('data=' + postArray);
}
}
<? if ($id == $user) : ?>
<div class="modalSetPro" onclick="setProImage(<? echo $picID; ?>,<? echo $uid; ?>)">Set Profile</div>
<div class="modalSetBac" onclick="setProCover(<? echo $picID; ?>,<? echo $uid; ?>)">Set Background</div>
<div class="modalDelImg" onclick="delItemPre(<? echo $picID; ?>, 1, <? echo $uid; ?>)">Delete</div>
<? endif; ?>
function delItemPre(itemID, type, user) {
var modArr = [itemID, type, user];
modArr = JSON.stringify(modArr);
$("#LoadMe").load('modals/del_modal.php?p=' + modArr);
}
【问题讨论】:
-
"你能给我一个简单的解释吗?"不。也许如果你提供一些代码,我们可以给你一些方向。
标签: javascript php jquery mysql ajax