alexis
alexis

Reputation: 9

Compare ip address in php

I made a table in my mysql where I store the login details of users. We store, ip address (as long2ip), username, sitename and time. What I would like to do is to check if a user is logged into a site with more than 2 different ip's.

Upvotes: 0

Views: 1399

Answers (2)

Patrick Georgi
Patrick Georgi

Reputation: 698

Sounds like this should be done in SQL, not PHP:

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

If necessary this can be further restricted on time (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).

Upvotes: 1

Joel Etherton
Joel Etherton

Reputation: 37543

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.";

Upvotes: 4

Related Questions