简体   繁体   English

从mysql中删除数据最安全的方法是什么? (PHP / MySQL的)

[英]What's the safest way to remove data from mysql? (PHP/Mysql)

I want to allow users as well as me(the admin) to delete data in mysql. 我想允许用户和我(管理员)删除mysql中的数据。 I used to have remove.php that would get $_GETs from whatever that needed to be deleted such as... remove.php?action=post&posting_id=2. 我曾经有过remove.php,可以从需要删除的内容中获取$ _GETs,例如... remove.php?action = post&posting_id = 2。 But I learned that anyone can simply abuse it and delete all my data. 但我了解到任何人都可以简单地滥用它并删除我的所有数据。

So what's the safest way for users and me to delete information without getting all crazy and hard? 那么对于用户和我来说,删除信息的最安全方法是什么,而不是让所有人都疯狂而艰难? I am only a beginner :) I'm not sure if I can use POSTs because there is no forms and the data isn't changing. 我只是一个初学者:)我不确定我是否可以使用POST,因为没有表格,数据没有变化。 Is sessions good? 会议好吗? Or would there be too many with postings, user information, comments, etc. 或者会有太多的帖子,用户信息,评论等。

Ex: James wants to delete one of his postings(it is posting_id=5). 例如:James希望删除他的一个帖子(post_id = 5)。 So he clicks the remove link and that takes him to remove.php?action=post&posting_id=5. 所以他点击了删除链接,然后他将删除.php?action = post&posting_id = 5。

EDIT: Alright, so now I am a little confused. 编辑:好的,所以现在我有点困惑。 While I can't be 100% secure, how do I do this with $_POSTs? 虽然我无法100%安全,但我如何使用$ _POSTs执行此操作? SOO I should use GETs to get all the data to remove.php, THEN have a confirmation submit button and when users click on it, it put all the data into POSTs and delete from the dbc? SOO我应该使用GET来获取所有数据remove.php,然后有一个确认提交按钮,当用户点击它时,它将所有数据放入POST并从dbc中删除?

Deleting records is a kind of a scary practice. 删除记录是一种可怕的做法。 If you or someone makes a mistake there's no real recourse to resolve the issue. 如果您或某人犯了错误,则无法真正解决问题。 Expunged records are very hard to resurrect. 清除记录非常难以复活。

Instead of deleting records, you could add an "active" bit (eg Boolean) column that is toggled off when users "delete" records. 您可以添加“活动”位(例如布尔值)列,而不是删除记录,当用户“删除”记录时,该列会切换为关闭。 Essentially your users would be suspending records by toggling them off and the records would be saved in case mistakes or abuse but appear "deleted" to the user. 基本上,您的用户将通过切换来暂停记录,并且在出现错误或滥用的情况下保存记录,但会向用户显示“已删除”。 To make this work with your other queries, just add a where clause of active = 1. 要使其与其他查询一起使用,只需添加active = 1的where子句即可。

You could then have a utility script that's run at some specific date interval that would clean out deprecated, past dated records. 然后,您可以拥有一个在某个特定日期间隔运行的实用程序脚本,该脚本将清除已弃用的过去日期记录。 You'd also need some type of timestamp for this type of maintenance. 您还需要某种类型的时间戳来进行此类维护。

Just a thought. 只是一个想法。 Take if for what it's worth. 考虑一下它的价值。

I'll echo gurun8 in preferring to 'mark' records as deleted, instead of actually removing data. 我会回应gurun8更喜欢将'标记'记录删除,而不是实际删除数据。 And then obviously, you'll need to check that the authenticated user has permission to delete the post. 显然,您需要检查经过身份验证的用户是否有权删除帖子。

However, it seems very important to mention that $_GET is not safe even with authentication because of cross-site request forgery . 然而,似乎重要的一提的是$_GET是不是安全的,即使有 ,因为认证 跨站请求伪造

Imagine if Amazon adding things to your cart based on a GET request. 想象一下,如果亚马逊根据GET请求向您的购物车添加内容。 All I'd have to do is put an image on my page with that URL, and everyone who visited that page and logged into Amazon will have products added automatically. 我所要做的就是在我的页面上放置一个带有该URL的图像,访问该页面并登录到亚马逊的所有人都将自动添加产品。

To match your example, I don't like Jame's post, so i put an image on my site like this: 为了匹配你的例子,我不喜欢Jame的帖子,所以我在我的网站上放了一个像这样的图像:

<img src='http://example.com/remove.php?action=post&posting_id=5'>

And I send him a link to my page, and ask him to check it out, hoping that at the time he's logged in to your site. 我向他发送了一个指向我页面的链接,并要求他查看,希望当时他登录到您的网站。 Because, of course, he clicked that little 'keep me logged in' button. 因为,当然,他点击了那个“让我登录”按钮。

So you are right to be concerned about using GET . 所以你关心使用GET是正确的 If you don't want to litter pages with forms, then confirm the action by POST . 如果您不想使用表单乱丢页面,请通过POST确认操作。

Well you have to start by authenticating the users with a login script. 那么你必须首先使用登录脚本验证用户。

If you want the simplest solution possible, then I'd suggest protecting the directory in which you have remove.php with a simple .htaccess username and password . 如果你想要最简单的解决方案,那么我建议用一个简单的.htaccess 用户名和密码来保护你的remove.php目录。

If different users have different rights for deleting database entries, then you probably should create a PHP login script and use PHP session. 如果不同的用户具有删除数据库条目的不同权限,那么您可能应该创建PHP登录脚本并使用PHP会话。

Bonk me if I'm stupid, but I searched for quite some time for a simple PHP login tutorial that could be placed on a real site (doesn't use session_register(), uses mysql_real_escape_string(), htmlspecialchars() etc) and I simply couldn't find one! 如果我是愚蠢的话,请加油,但我搜索了一段时间,可以将一个简单的PHP登录教程放在一个真实的网站上(不使用session_register(),使用mysql_real_escape_string(),htmlspecialchars()等)和我根本找不到一个!

Probably this one comes the closest, you just have to replace session_register() variables with $_SESSION ones for it to work without register_globals (default in PHP5). 可能这个最接近,你只需用$ _SESSION替换session_register()变量就可以在没有register_globals的情况下工作(默认在PHP5中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP输入验证和处理MySQL错误:最安全的方法是什么? - PHP Input Validation and Handling MySQL Errors: What's The Safest Route? 使用PHP在MySQL中保存意外时间格式的最安全方法 - The safest way for saving unexpected time formats in MySQL with PHP 使用PHP / MySQL封装数据访问的好方法是什么? - What's a good way to encapsulate data access with PHP/MySQL? 将数据/ SQL负载写入文本文件的最安全方法是什么? - What's the safest way to write loads of data / SQL to a text file? 在PHP 5.3中使用PDO(输入和输出数据)的最安全的方法是什么 - What is the safest way to use PDO (in & outgoing data) in PHP 5.3 在 PHP 中访问 CLI 程序的最安全方法是什么 - What is the safest way to access CLI program in PHP 共享对象最安全的方法是什么? - What is the safest way to share objects PHP 从php应用程序写入/ etc的最安全方法? - Safest way to write into /etc from php application? 在Android上向PHP / MySQL后端发送和检索(可能是大量)数据的最佳方法是什么? - What's the best way to send and retrieve (possibly large amounts) data to a PHP/MySQL backend on Android? 在不使用枚举的情况下,从 MySQL 的一组预选数据中输入数据的最佳方法是什么? - Without using enums, what's the best way to enter data from a set of preselected data for MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM