简体   繁体   English

比较 php 中的 ip 地址

[英]Compare ip address in php

I made a table in my mysql where I store the login details of users.我在我的 mysql 中创建了一个表格,用于存储用户的登录详细信息。 We store, ip address (as long2ip), username, sitename and time.我们存储 ip 地址(作为 long2ip)、用户名、站点名称和时间。 What I would like to do is to check if a user is logged into a site with more than 2 different ip's.我想做的是检查用户是否登录到具有超过 2 个不同 ip 的站点。

This should be fairly easy as long as you have the database stored version available.只要您有可用的数据库存储版本,这应该相当容易。 Some untested pseudocode:一些未经测试的伪代码:

$longIPdb = ip2long($ipfromdb);
$longRemote = ip2long($_SERVER['REMOTE_ADDR']);

if ($longIPdb == $longRemote) echo "We're the same.";
else echo "We're from different IPs.";

Sounds like this should be done in SQL, not PHP:听起来应该在 SQL 中完成,而不是在 PHP 中完成:

SELECT COUNT(DISTINCT ip) WHERE username='username';

If necessary this can be further restricted on time ( AND time > NOW()-3600 ).如有必要,这可以进一步限制时间( AND time > NOW()-3600 )。 The result will be the number of different IP addresses the given user has in your database (in the given time frame, if specified).结果将是给定用户在您的数据库中拥有的不同 IP 地址的数量(在给定的时间范围内,如果指定)。

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

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