数据库:
pub_id pub_name time ip
1 King 1359500087 3388636152
2 Queen 1359550082 6385394932
3 Jack 1359502084 5648646562
4 Heart 1359524083 9283834142
5 Jim 1359503082 3388636152
|_ Using time() |_ Using ip2long()
PHP/MySQLi 代码:
$pub_id = $_GET['pub_id'];
$pub_name = $_GET['pub_name'];
$ip = ip2long($_SERVER['REMOTE_ADDR']);
$time = time();
$query = $db->query("SELECT * FROM impressions WHERE pub_id = '$pub_id' AND pub_name = '$pub_name' AND ip = '$ip' AND time >= '???'");
$ip_adderss = $query->num_rows;
$query->close();
if($ip_adderss == 0){
// Redirect
} else{
// Do nothing
}
现在使用 time() 和 IP 我如何检查当前访问者上次访问是否是 24 小时前?
一个简单的解决方案是使用 if 语句来检查 ip 时间和现在时间。
// Check if the IP is new or returning
$query = $db->query("SELECT * FROM impressions WHERE pub_id = '$pub_id' AND pub_tag = '$pub_tag' AND month = '$month' AND day = '$day' AND ip = '$ip' AND domain = '$domain' AND valid = '1' ORDER BY id DESC");
$ip_address = $query->num_rows;
if($ip_address == 0){
$ip_ok = 1;
} elseif($ip_address != 0){
$impression = $query->fetch_assoc();
if($time >= ($impression['time'] + 86400)){
$ip_ok = 1;
} else{
$ip_ok = 0;
}
} else{
$ip_ok = 0;
}