0

我试图阻止来自同一 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 !");
}

这行得通,但是如果我想允许每个 IP 有两个帐户怎么办?我试过了:

$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 !");
}

不工作。不允许使用第二个帐户。

4

2 回答 2

2

你需要数一下。使用 count()

SELECT count(*) as count FROM users WHERE ip = :ip

http://php.net/manual/en/pdostatement.rowcount.php

于 2013-01-08T11:18:51.410 回答
1

只需更新如下查询:

$stmt = $db->prepare("SELECT count(*) FROM users WHERE ip = :ip");
于 2013-01-08T11:52:55.670 回答