【发布时间】:2022-01-27 17:51:12
【问题描述】:
好的,所以我有一个 ajax 请求,它指向一个名为 inc/ajax/del_images.php 的文件,该文件删除的是用户选择删除的图像
Edit_post.php:
<form class="form-control" action="" autocomplete="off" method="post" enctype="multipart/form-data">
<img id="img" src="some url from database">
<button id="delete-img" data-id="W12kwd2">Delete img</button>
<img id="img" src="some url from database">
<button id="delete-img" data-id="T93pm3P">Delete img</button>
</form>
data-id是数据库表中img的id
还有图片和按钮以及来自 php 的 gen,我没有包括在内,因为它没有为帖子增加任何价值
edit_post 上的 Ajax:
$("#delete-img").on('click', function() {
$.ajax({
url: 'inc/ajax/del_images.php',
type: "POST",
data: {
img_id: $(this).attr("data-id")
},
cache: false,
success: function(result){
console.log(result);
}
});
)};
然后在del_images.php:
session_start();
if(isset($_POST['img_id'])){
//image id
$iid = $_POST['img_id'];
//let's check if this image id is valid/in the database
require("conn_user.php");
$stmt = $conn->prepare("SELECT * FROM `images` WHERE `ID` = ?");
$stmt->bind_param("s", $iid);
$stmt->execute();
$stmt_results = $stmt->get_result(); // get result
$row_get = $stmt_results->fetch_assoc();
if($stmt_results->num_rows > 0){
//img with the id was found
//now check if the current user is the owner of post with post[ID] related to the image[ID]
$stmt = $conn->prepare("SELECT * FROM `posts` WHERE `ID` = ?");
$stmt->bind_param("s", $row_get['post_id']);
$stmt->execute();
$stmt_results = $stmt->get_result(); // get result
$row_get_post = $stmt_results->fetch_assoc();
if($stmt_results->num_rows > 0){
//post was found lets check $_SESSION with poster id(in DB)
if($_SESSION['uid'] == $row_get_post['poster_id']){
//this means the current user is the owner of post aswell as the image
//now delete the image cuz the user is the owner which means its safe
$stmt = $conn->prepare("DELETE FROM `images` WHERE ID = ?");
$stmt->bind_param("s", $iid);
$stmt->execute();
$delete_results = $stmt->store_results(); // get result
if($delete_results->affected_rows == 1){
//image was deleted return info so page
print_r('image deleted!');
}else{
print_r('image could not be deleted!');
}
}else{
//id didnt match prop a hacker so force kick and admin review
//code removed for this post
}
}else{ //post not found this will never happen but if it does just add error output }
}else{
//img not found please tell the user
//this code was removed for simplicity of the post
}
}
我的数据库:
图片表
| ID | post_ID | url |
| :--------:| :--------:|:--------:|
| W12kwd2 | 1 | mNDNJD3324kmWD382n3r.png |
| T93pm3P | 1 | In3u2n329dnjJDEJKDde.jpg |
| Wo90dmp | 2 | JNMduwio3232ndsakdew.jpeg|
帖子表
| ID | post_title | poster_id |
| :--------: | :--------: |:--------: |
| 1| What a title | 1 |
| 2| Can you code?| 1|
| 3| Ajax, why and how | 4 |
我的问题:
The issue
所以另一个用户不能删除另一个用户的图片因为我正在检查他们是否也是与图片相关的帖子的所有者,但是假设用户正忙于编辑帖子 1,编辑帖子的 URL 将如下所示edit_post?post_id=1 这很好,但用户可以在按钮data-id 中插入与post ID 2 相关的图像的 ID 并删除它们,因为他也是帖子 ID 2 的所有者(您可以从 db 示例中看到它)现在首先我想让我们从 url 中获取 id,但是任何知道前端如何工作的白痴都可以检查 js 以插入他们想要的 url id= 的值,所以我该如何限制这个以便用户只能删除他们当前正在编辑的帖子的图像,而无需使用前端供应 ID
我可能很难使用 $_SESSION['current_edit'] = “他们点击编辑的帖子的当前 ID”,但问题导致他们有多个标签,因为他们编辑的帖子比我知道我需要的帖子更多使用某种类型的提供的 ID,但我如何将其锁定,以便用户在编辑另一个帖子时无法删除他们拥有的其他帖子的图像。
页脚注释* 如果我需要提供更多信息并编辑帖子以更清楚更具体,请告诉我,我会这样做,因为我知道 StackOverflow 是一个干净且维护良好的网站〜祝你有美好的一天:)
【问题讨论】:
-
只需将
AND POST_ID = :postId添加到您的删除查询中? -
但是 postId 来自哪里?通过 ajax 什么的?
-
@AlexHowansky 因为从查询中获得的 postId 纯粹基于图像的 id,所以如果他们提交 post ID 2 image id 那么它会得到 2 的 post id,只需检查一下用户是帖子 id 2 的所有者,用户是所有者,因此不会阻止用户删除与他/她拥有的其他帖子相关的图像
-
为什么要限制用户以这种方式删除图像?如果他们想编辑 HTML/JS 并尝试以这种方式删除图像,您将很难阻止它们。正如您所提到的,您真的想阻止他们弄乱其他人的图像。
-
@waterloomatt 好吧,我检查了我的一些竞争对手,它们是 OLX 和 Ebay 等。他们都有某种系统来检查用户不能删除他们拥有的其他帖子图像,所以我的想法是它应该可能我只是找不到办法。这就是为什么我要求看看其他开发人员是否有办法做到这一点。但我想我只是检查一下用户是否删除了其他帖子图片,该帖子将进入审核以供管理员审核和批准或其他内容