简体   繁体   English

MySQL/PHP 请求操作

[英]MySQL/PHP request manipulation

First time posting.第一次发帖。

I manage a website that handles certain types of transactions for virtual currency.我管理一个处理某些类型的虚拟货币交易的网站。 It is a php/mysql web application.它是一个 php/mysql 网络应用程序。 Recently we've had a user somehow withdraw the same amount (essentially duplicating their virtual money) 6 or 7 times (until we ran out of funds).最近,我们有一个用户以某种方式提取了 6 到 7 次相同数量的(基本上是复制了他们的虚拟货币)(直到我们用完资金)。 Looking at the log, the transactions were processed milliseconds apart, so I'm assuming that they user had for example 5,000 funds and requested to withdraw them by spamming the request in order to attempt to withdraw more than they owned.查看日志,交易的处理间隔为毫秒,因此我假设他们的用户拥有例如 5,000 笔资金,并要求通过发送垃圾邮件请求来提取它们,以尝试提取比他们拥有的更多的资金。

How could I go about preventing this from happening in the future, and how could I test this, or repeat this process myself?我怎样才能防止将来发生这种情况,我怎样才能测试这个,或者自己重复这个过程?

Thanks for any help.谢谢你的帮助。

I don't think this is a typical question on here, I'm sorry.我不认为这是这里的典型问题,对不起。 I'm not a developer, my current developer is on leave, so he's refused to assist.我不是开发人员,我现在的开发人员正在休假,所以他拒绝提供帮助。

One way this could be managed would be to force a certain time interval to pass at a timestamp level (ie the user cannot process multiple transactions within x minutes).可以管理的一种方法是强制在时间戳级别传递某个时间间隔(即用户无法在 x 分钟内处理多个事务)。

To do this, and assuming you'd be inserting into your table the transaction stats then apply it accordingly, you can force a check constraint on new insertions that will reject any row not respecting your timestamp condition为此,并假设您将事务统计信息插入到您的表中,然后相应地应用它,您可以强制对新插入进行检查约束,这将拒绝任何不符合您的时间戳条件的行

Some idea to avoid to spam request :避免垃圾邮件请求的一些想法:

FRONT SIDE :正面

  • Disable the button that allow user to click and spam the request.禁用允许用户单击并发送垃圾邮件请求的按钮。
  • When user click replace the button by some loading icon当用户单击某些加载图标替换按钮时

BACK SIDE :背面:

  • Create a temporary file when the transaction begin (or edit one file to add some information about the transaction) and when the transaction end delete the file (or the data).在事务开始时创建一个临时文件(或编辑一个文件以添加有关事务的一些信息),并在事务结束时删除该文件(或数据)。 So before EACH transaction, you check if you have the file / data : if you have something -> no transaction because one is already running.所以在每个事务之前,你检查你是否有文件/数据:如果你有东西 -> 没有事务,因为一个已经在运行。 Some documentation about this : http://php.net/manual/fr/function.file-put-contents.php关于此的一些文档: http : //php.net/manual/fr/function.file-put-contents.php

  • An other idea is to add some param in your database (or create some table like user_transaction) and when you start the transaction you create a user_transaction row (or change a param from 0 to 1 as you want) and when the transaction end you delete the row or change the param from 1 to0.另一个想法是在您的数据库中添加一些参数(或创建一些表,如 user_transaction),当您开始事务时,您创建一个 user_transaction 行(或根据需要将参数从 0 更改为 1),并在事务结束时删除行或将参数从 1 更改为 0。 So before EACH transaction you check if a row exist for this user or if the param is 1 : if yes -> no transaction because one is already running.因此,在每个事务之前,您检查该用户是否存在一行,或者参数是否为 1 :如果是 -> 没有事务,因为一个已经在运行。 Nothing complicated here, I have no information about your database so can't do more :)这里没有什么复杂的,我没有关于你的数据库的信息,所以不能做更多:)

Try to add one lock from FRONT + one lock from BACK and you should reduce the problem !尝试从 FRONT 添加一个锁 + 从 BACK 添加一个锁,您应该减少问题!


EDIT : add some tracking编辑:添加一些跟踪

To avoid user to spam request, you can add some tracking to avoid fraude :为避免用户发送垃圾邮件请求,您可以添加一些跟踪以避免欺诈:

  • You can add some counter and add +1 each time user click to know if he spam您可以添加一些计数器并在每次用户点击时添加 +1 以了解他是否垃圾邮件
  • You can create a table in database to know each time a user send a request to know which user click, when and how much currency for example您可以在数据库中创建一个表,以了解每次用户发送请求以了解哪个用户点击、何时点击以及使用多少货币

This way you know who fraude (who click a lot to spam request or who send to many request in few times) and you can send email or warning message for example, or if you can track the amount of currency he win by "cheating", ask him to give it back I don't know how your app works !通过这种方式,您可以知道谁在欺诈(谁点击了很多垃圾邮件请求或谁多次发送了多次请求),并且您可以发送电子邮件或警告消息,或者您是否可以通过“欺骗”跟踪他赢得的货币数量,让他还给我我不知道你的应用程序是如何工作的!

A SQL to withdraw funds should be:提取资金的 SQL 应该是:

   UPDATE balance
   FROM users
   SET balance = balance - $amount
   WHERE id = $id AND balance >= $amount

If there is 0 rows affected then there is insufficient balance.如果有 0 行受影响,则余额不足。

Any solution that tests balance before hand is susceptible to race conditions.任何事先测试平衡的解决方案都容易受到竞争条件的影响。

如果余额的类型是整数或大整数,则将其设为无符号将防止其变为负数。

ALTER TABLE user MODIFY balance BIGINT UNSIGNED NOT NULL;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM