PHP 在来自同一 IP 的 4 个请求后执行某些操作

发布于 2025-01-11 22:17:42 字数 950 浏览 2 评论 0原文

当同一个 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 }

?>

Would it be possible via PHP to do something after a same IP address has accessed to the site for 4 times or more?

I can't use cookies as this rule needs to prevail even if using incognito mode or switching a browser if possible.

I managed to do it so it would show something after visiting more than 1 timed; but can't figure out how to achieve the specified before.

<?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 }

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沒落の蓅哖 2025-01-18 22:17:42

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

现在创建一个简单的查询来检查数据库中是否存在该 IP。如果 ip 存在,则更新 ip 表中的 counts 列,将其添加为 1。如果不存在,则将 ip 和 counts 添加到 1。

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

注意:不同的用户可能拥有相同的 IP 地址。

Just create a new table with a column that stores ip records. Then create another column called counts or whatever with default value of 0. This will show how many times a user has visited your site.

Now create a simple query that checks if the ip exists in database. If the ip exists then update the counts column in ip table by adding it with 1. If it does not exist, add the ip and counts to 1.

Finally check if counts column contains value > 4 then do what you want!

NB: Different users might have the same ip address.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文