dongwenghe2416 2016-01-28 17:55
浏览 41
已采纳

防止记录ID操作

How do you prevent a malicious user from changing URL or form data, specifically a record ID. For example:

http://example.com/deleteproduct.php?id=34

The user could change the ID value from 34 to say 69 and in doing so delete a record belonging to another customer. I guess the obvious protection is to validate the ID before performing the delete to make sure the user has access to that record but is there perhaps another approach that is consider better practice? The downside of validating the ID requires more database queries which would be great to avoid.

  • 写回答

3条回答 默认 最新

  • duanbogan5878 2016-01-28 18:20
    关注

    I guess the obvious protection is to validate the ID before performing the delete to make sure the user has access to that record.

    This is the only way to ensure that your user has access to delete these rows.

    The downside of validating the ID requires more database queries which would be great to avoid.

    Not necessarily. You can simply check when you're deleting to only delete rows that belong to your user.

    For example, assuming your table structure looks similar to:

    users
    -----
    id | username
    1  | Dave
    2  | John
    
    products
    -----
    id | name | user_owner
    1  | Milk | 1
    2  | Cake | 2
    

    So if Dave visited deleteproduct.php?id=2, the following query would execute:

    DELETE FROM products WHERE id = 2 AND user_owner = 1;
    

    It wouldn't delete anything, and $mysqli->affected_rows would return zero.

    When affected rows is zero it means that the product ID was invalid or the product didn't belong to the user, either way: you would display a message telling the user that the product id is invalid.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题