除了通过Shoutbox 应用程序的IP 地址之外,如何区分用户?
我正在使用 php、jQuery 和 AJAX 进行编码来创建一个shoutbox 应用程序。到目前为止,我通过 IP 地址记录不同的用户,以便分配不同的默认用户名,并根据相应的 IP 地址存储用户选择的用户名。 (shoutbox 不是基于登录的,并且在访问网站时首次登录时会自动分配一个访客用户名,并且用户可以将此默认访客用户名更改为其他名称)
问题是,当来自同一网络的多个人使用相同的IP地址,我的shoutbox将无法区分不同计算机上的用户。有没有办法区分同一网络下的IP?或者如果我使用 PHP 会话或 cookie,这也能解决问题吗?最主要的是我希望能够将用户创建的用户名存储在适当的计算机中,这样用户下次访问我的shoutbox 网页时就不会看到默认的访客用户名。
I'm coding in php, jQuery, and AJAX to create a shoutbox application. So far I'm recording different users through their IP address so as to assign different default usernames and to store user-selected usernames according to their appropriate IP address. (the shoutbox is not login-based and assigns an automatic guest username on first sign-in when visiting the website and the user can change this default guest username to something else)
The problem is that when there are multiple people from the same network using the same IP address, my shoutbox won't be able to differentiate between the users on the different computers. Is there a way to differentiate between IPs under the same network? Or if I use a PHP session or cookie, will that solve the problem too? The main thing is I want to be able to store the user-created usernames in the appropriate computers so the user is not presented with the default guest username next time he or she visits my shoutbox webpage.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以为每个 IP 地址分配一个 id。所以第一个人会被分配id=1。第二个具有相同的 IP 地址,id=2,依此类推。
You could assign an id to each ip-address. So the first person will be assigned id=1. The second with the same ip-address, id=2, and so on.
检查由防火墙代理设置的“X-FORWARDED-FOR”的 HTTP 标头,以识别另一端个人的 IP 地址。 http://en.wikipedia.org/wiki/X-Forwarded-For
检查他们的计算机上是否有特定的 cookie,如果没有,那么他们是不同的用户(当然,尽管他们可能刚刚清除了 cookie)
Check the HTTP header for 'X-FORWARDED-FOR' which is set by firewall proxies to identify the IP address of the individual on the other side. http://en.wikipedia.org/wiki/X-Forwarded-For
Check for a specific cookie on their computer, and if there isn't one present then they're a different user (although, of course, they may have just cleared their cookies out)
是的,您可以将他们的用户名写入 cookie。因此,他们将使用相同的用户名进行连接,直到 cookie 文件过期,或者重新安装浏览器(或操作系统),或者清除浏览器数据,或者干脆使用另一台计算机。
或者,如果可以将用户名保存在数据库中,您还可以通过 IP 加上浏览器的一些标头(例如,用户代理)来区分用户。当然,在这种情况下,用户名保持不变,直到用户更新或重新配置浏览器,或者使用另一个浏览器或另一台计算机。或者,如果他或她的 IP 发生变化(例如,许多 ISP 使用动态 IP,并且用户可以通过 Wi-Fi 从咖啡馆进行连接),
您也可以将这些方式结合起来。例如,将用户名保存到cookie中,同时将相同的用户名与IP和用户代理一起保存到服务器中。如果 cookie 可用,请使用 cookie 中的用户名。如果不是,请尝试使用该人的 IP 和用户代理从数据库中恢复该人的用户名。即使这不准确并且您分配了错误的用户名,用户仍然可以将其更改为正确的用户名。
Yes, you can write their usernames to cookies. So they’ll connect with the same username until their cookie files expire, or they reinstall their browser (or OS), or clear the browser data, or simply use another computer.
Or, if it’s okay to keep the usernames in a database, you can also differ users by IP plus some headers from browsers (for example, the user agent). Of course, in this case the username remains the same until the user updates or reconfigures the browser, or uses another browser or another computer. Or if his or her IP changes (a lot of ISPs use dynamic IPs, and also the user can connect from a café via Wi-Fi, for example)
You can also combine these ways. For example, save the username to cookies, and at the same time, save the same username to the server with IP and user agent. If a cookie is available, use the username from the cookie. If it’s not, try to recover the person’s username from the database using his IP and user agent. Even if that wasn’t accurate and you assigned a wrong username, the user still can change it to the proper one.