给定表单中的网站访问数据session_id, ip, user_agent
,以及可选的时间戳,遵循以下条件,您如何最好地将会话聚集成唯一访问者?
session_id
: 是给每个新访客的 ID。它不会过期,但是如果用户不接受cookies/清除cookies/更改浏览器/更改设备,他将不再被识别
IP
可以在不同用户之间共享(想象一个免费的 Wi-Fi 咖啡馆,或者您的 ISP 重新分配 IP),他们通常至少有 2 个家庭和工作场所。
User_agent
是浏览器+操作系统版本,允许区分设备。例如,用户可能同时使用手机和笔记本电脑,但不太可能使用 windows+apple 笔记本电脑。同一个会话 ID 不太可能有多个用户代理。
数据可能看起来像这里的小提琴: http ://sqlfiddle.com/#!2/c4de40/1
当然,我们谈论的是假设,但它是关于尽可能接近现实。例如,如果我们在有限的时间范围内遇到具有不同 session_id 的相同 ip 和 useragent,则可以公平地假设它是同一用户,但有一些极端情况例外。
编辑:解决问题的语言是无关紧要的,它主要是关于逻辑而不是实现。伪代码很好。
编辑:由于小提琴的缓慢性质,您可以选择读取/运行 mysql:
select session_id, floor(rand()*256*256*256*256) as ip_num , floor(rand()*1000) as user_agent_id
from
(select 1+a.nr+10*b.nr as session_id, ceil(rand()*3) as nr
from
(select 1 as nr union all select 2 union all select 3 union all select 4 union all select 5
union all select 6 union all select 7 union all select 8 union all select 9 union all select 0)a
join
(select 1 as nr union all select 2 union all select 3 union all select 4 union all select 5
union all select 6 union all select 7 union all select 8 union all select 9 union all select 0)b
order by 1
)d
inner join
(select 1 as nr union all select 2 union all select 3 union all select 4 union all select 5
union all select 6 union all select 7 union all select 8 union all select 9 )e
on d.nr>=e.nr