【问题标题】:PHP delete image related to post IDPHP删除与帖子ID相关的图像
【发布时间】: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 等。他们都有某种系统来检查用户不能删除他们拥有的其他帖子图像,所以我的想法是它应该可能我只是找不到办法。这就是为什么我要求看看其他开发人员是否有办法做到这一点。但我想我只是检查一下用户是否删除了其他帖子图片,该帖子将进入审核以供管理员审核和批准或其他内容

标签: php ajax


【解决方案1】:

delete image from folder PHP

这篇文章可能会对你有所帮助。

你需要做什么。

  • 使用ajax从数据库中查询id
  • 然后获取 url 列。
  • 通过取消您调用的 url 行与您的文件系统的链接来删除文件。

这就是全部过程。

【讨论】:

  • 这仍然行不通,因为这不是我的问题,但感谢您的尝试
  • 那我可以给你一个建议。如果您知道每张照片属于哪个用户,则应将过程更改如下; - 不要只发送 img_id,您应该始终发送 img_id 和 user_id。如果img_id属于user_id,则完成删除。如果它不属于,请不要这样做。
  • @ibrahim,如果用户将 HTML/JS 中的 userId 更改为其他人会发生什么?然后他们可以删除其他人的图像吗?
  • 没有。如果您将 user_id 分配给与 PHP 的会话,即使用户更改了 js 文件中的 img_id,您也会知道后端的所有内容。因此,无论情况如何,您都将掌控一切。 stackoverflow.com/questions/41879519/… Rajdeep Paul 的解决方案会帮助你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-12
  • 2015-02-28
  • 2011-08-20
  • 2015-03-18
  • 1970-01-01
  • 2014-08-02
相关资源
最近更新 更多