I’m trying to prevent multiple accounts from the same IP:
if (! empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif (! empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip = $_SERVER['REMOTE_ADDR'];
}
$stmt = $db->prepare("SELECT 1 FROM users WHERE ip = :ip");
$stmt->bindParam(':ip', $ip, PDO::PARAM_STR);
$result = $stmt->execute();
$row = $stmt->fetch();
if ($row)
{
die("Only one account per IP is allowed !");
}
This works, but what if I want to allow two accounts per IP ? I tried:
$stmt = $db->prepare("SELECT * FROM users WHERE ip = :ip");
$stmt->bindParam(':ip', $ip, PDO::PARAM_STR);
$result = $stmt->execute();
$row = $stmt->fetch();
if ($row > 1)
{
die("Only one account per IP is allowed !");
}
Doesn’t work. The second account is not allowed.
You need count it.Using count()
http://php.net/manual/en/pdostatement.rowcount.php