PHP会在同一IP发出4个请求后执行某些操作



在同一IP地址访问网站4次或以上后,是否可以通过PHP执行某些操作?

我不能使用cookie,因为即使使用隐姓埋名模式或在可能的情况下切换浏览器,也需要遵守此规则。

我设法做到了,这样它在访问超过1次后就会显示一些东西;但是不知道如何实现之前指定的。

<?php
function allowedIP() {
$vFile = 'vfile'; // file to store visitor data
$revisit = 3600*24*60; // not allowed time in seconds
$now = time(); // visit time
$vIP = ip2long( $_SERVER['REMOTE_ADDR'] ); // get the ip and convert to long
$vData = ( file_exists( $vFile ) ) ?
unserialize( file_get_contents( $vFile ) ) : array(); // get the visit data
if( ! isset( $vData[$vIP] ) || $now - $vData[$vIP] > $revisit ) {
// first visit or 60 days passed since the first visit
$vData[$vIP] = $now; // store the visit time
file_put_contents( $vFile, serialize( $vData ) ); // save to file
return true;
}
return false;
}
if( ! allowedIP() ) { ?>
<p>Paywall</p>
<?php }
?>

只需创建一个带有存储ip记录的列的新表。然后创建另一个名为count的列或其他默认值为0的列。这将显示用户访问您的网站的次数。

现在创建一个简单的查询,检查ip是否存在于数据库中。如果ip存在,则通过添加1来更新ip表中的计数列。如果不存在,则添加ip并计数为1。

最后检查计数列是否包含值>4然后做你想做的事!

注意:不同的用户可能有相同的ip地址

最新更新