是否可以在此代码中创建重复的IP? [英] is it possible creation of duplicate ip in this code?

查看:54
本文介绍了是否可以在此代码中创建重复的IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个弹出式旋转器,该弹出式旋转器每天计算unic用户的ip(同一日期不允许用户重复IP)

I have a popup rotator that count unic user's ip per day(NO duplicate ip is allowed for user in same date)

$userip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$date = date('Y-m-d');
$result = mysql_query("SELECT `ip` FROM  `popupip` where ip = '$userip' AND userid = $secid AND date='$date'");
$num = mysql_num_rows($result);
    if ($num > 0) {
    // **it is duplicate Do not insert it in popupip table**
    }else{
    // **it is not duplicate ip insert it in popupip table**
    }

上面的代码是一个例子.我知道完整的代码.

above code is an example.I know full code.

但是当我查看phpmyadmin popupip表时,该用户有一些重复的IP(同一日期该用户的IP地址完全相同)

but when I look to my phpmyadmin popupip table there is a few duplicate ip for user (the exact same ip address for the user on the same date)

怎么可能?

其他信息: popupip userid中的int(11),date是日期类型,如2014-05-30",而ip是varchar. 此页面可能会通过弹出页面尽可能快地同时打开".用户同时快速打开页面和重复的ip创建之间有关系吗? MySQL中有错误吗? (也许是个大错误!!!)

extra information: in popupip userid is int(11) , date is "date type like 2014-05-30" and ip is varchar. this page may opens "as fast as possible at the same time" by popup pages. is there a relation between openning a page fast at same time by a user and duplicate ip creation? is there a bug in MySQL? (maybe a big bug!!!!)

推荐答案

是的,有可能.这是种族条件的经典案例.

Yes, it is possible. It's a classical case of a race condition.

快速说明:

有两个机会同时 通过第一项检查,得到$num == 0并都插入新行.

There is a chance that 2 requests simultaneously pass the first check, get $num == 0 and both insert a new row.

要消除它,您需要创建一个覆盖(user_id, ip, date)列的UNIQUE约束

To eliminate it you need to create a UNIQUE constraint that covers (user_id, ip, date) columns

详细说明:

  • Wikipedia - Race Condition
  • Stackoverflow - What is a race condition?

这篇关于是否可以在此代码中创建重复的IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆