需要比较IP地址

时间:2015-10-08 14:13:55

标签: php mysqli

我有这个容器用户ip

$forum['User']['ipaddress'];

我有表线程和phpMyAdmin pid (线程ID)和 ipaddress (用户评论IP)

中的2列

如果用户在一个论坛帖子中写了2个帖子,则需要显示消息<p> Your ip repeated </ p>

enter image description here

$mysqli = new mysqli("localhost", "root", "root", "db");

    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

我该怎么做?

1 个答案:

答案 0 :(得分:0)

这应该做你想要的。 我还没有对这段代码进行测试,因为这并不容易,但你应该明白这一点。

//escaping to prevent sql injection
$pid = mysqli_real_escape_string($mysqli, $post_id);
$ipaddress = mysqli_real_escape_string($mysqli, $forum['User']['ipaddress']);

$query = "SELECT count(ipaddress) AS addresses_found FROM threads WHERE ipaddress = {$ipaddress} AND pid = {$pid}";

$count_ip_addresses = 0;
if ($result = $mysqli->query($query)) {

  $row = $result->fetch_assoc();
  $count_ip_addresses = $row['addresses_found']

  $result->free();
}

if($count_ip_addresses >= 2){
  echo '<p> Your ip repeated </ p>';
}