如何结束/终止检查表内IP地址而不影响其他功能的函数?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (53)

根据标题,我该如何完成它?检查已经存储在db中的IP地址的功能之一应该在不影响其他功能的情况下终止特定的检查功能。我曾经使用exit()和die()但它会停止所有功能。

main.php:

function CheckIfUserIpExist() {
    $connection = DBconnect();
    $user_ip = $_SERVER["REMOTE_ADDR"];
    $sql = "SELECT * FROM views WHERE user_ip = ?";
    $stmt = $connection->prepare($sql);
    $stmt->bind_param("s", $user_ip);
    $stmt->execute();
    $stmt->store_result();
    $users_ip = $stmt->num_rows;
    if($users_ip > 0) {
        die();
    }
}

function AddUserWhenPageIsViewed() {
    $connection = DBconnect();
    $user_ip = $_SERVER["REMOTE_ADDR"];
    $time_ = date('Y-m-d G:i:s');
    $sql = "INSERT INTO views (user_ip, time_) VALUES (?,?)";
    $stmt = $connection->prepare($sql);
    $stmt->bind_param("ss", $user_ip, $time_);
    $stmt->execute();
}

index.php

CheckIfUserIpExist(); 
AddUserWhenPageIsViewed();
提问于
用户回答回答于
function CheckIfUserIpExist() {
    $connection = DBconnect();
    $user_ip = $_SERVER["REMOTE_ADDR"];
    $sql = "SELECT * FROM views WHERE user_ip = ?";
    $stmt = $connection->prepare($sql);
    $stmt->bind_param("s", $user_ip);
    $stmt->execute();
    $stmt->store_result();
    $users_ip = $stmt->num_rows;
    if($users_ip > 0) {
        return false;
    }
    return true;
}

function AddUserWhenPageIsViewed() {
    $connection = DBconnect();
    $user_ip = $_SERVER["REMOTE_ADDR"];
    $time_ = date('Y-m-d G:i:s');
    $sql = "INSERT INTO views (user_ip, time_) VALUES (?,?)";
    $stmt = $connection->prepare($sql);
    $stmt->bind_param("ss", $user_ip, $time_);
    $stmt->execute();
}

当你调用测试函数时:

if ( ! CheckIfUserIpExist() ) { 
    AddUserWhenPageIsViewed();
}
用户回答回答于
function CheckIfUserIpExist() {
    $connection = DBconnect();
    $user_ip = $_SERVER["REMOTE_ADDR"];
    $sql = "SELECT * FROM views WHERE user_ip = ?";
    $stmt = $connection->prepare($sql);
    $stmt->bind_param("s", $user_ip);
    $stmt->execute();
    $stmt->store_result();
    $users_ip = $stmt->num_rows;
    if($users_ip > 0) {
        return;
    }
}

function AddUserWhenPageIsViewed() {
    $connection = DBconnect();
    $user_ip = $_SERVER["REMOTE_ADDR"];
    $time_ = date('Y-m-d G:i:s');
    $sql = "INSERT INTO views (user_ip, time_) VALUES (?,?)";
    $stmt = $connection->prepare($sql);
    $stmt->bind_param("ss", $user_ip, $time_);
    $stmt->execute();
}

所属标签

可能回答问题的人

  • 天使的炫翼

    15 粉丝531 提问35 回答
  • 旺仔小小鹿

    社区 · 运营 (已认证)

    48 粉丝0 提问27 回答
  • 富有想象力的人

    2 粉丝0 提问26 回答
  • 发条丶魔灵1

    6 粉丝525 提问25 回答

扫码关注云+社区

领取腾讯云代金券