每天限制登录用户对特定页面的http请求

时间:2016-07-22 作者:Peter Noges

我的WordPress页面上有一个资源,我每天只想为每个登录用户显示X次。我用的是216字新闻的主题。

目前,访客/访客/未登录用户必须通过重定向对我的网站进行身份验证,才能访问任何内容。

如何对每个登录用户每天或每小时施加http请求限制/页面浏览限制?我不想按IP地址来做,我想按唯一用户来做。

有人看到过吗?我看到的壁橱是:https://premium.wpmudev.org/forums/topic/technical-question-how-to-limit-the-number-of-page-views-for-a-specific-site-under-a-wpmu-instsall#post-384060 但是否已经有了解决方案?我搜索了throttle、page view、http、limit、restrict、expire,但似乎没有任何内容限制登录用户每天的内容。

非常感谢。

**更新:我正在覆盖默认页面。二十一世纪主题的php文件,为登录用户实现基本的页面视图限制**

   <?php
//首先,检查get\\u current\\u user\\u id。

$current\\u user=wp\\u get\\u current\\u user();如果(0==$当前用户->ID){//注销}其他{//登录。

    //Then create a log of visits by adding information to a user\'s metadata with update_user_meta & get_user_meta. 

    $COUNT_VISITS_TOTAL_LIMIT = 20;
    $countVisitsUsed = 0;

    if (get_user_meta($current_user->ID, \'countVisitsUsed\', true)) {
        $countVisitsUsed = get_user_meta($current_user->ID,\'countVisitsUsed\',true);
        update_user_meta($current_user->ID, \'countVisitsUsed\', $countVisitsUsed+1); //increment
    }else{
          update_user_meta( $current_user->ID, \'countVisitsUsed\', 1, false );
          $countVisitsUsed = get_user_meta($current_user->ID,\'countVisitsUsed\',true);
    }


    if ($countVisitsUsed == null || $countVisitsUsed ==false || $countVisitsUsed < $COUNT_VISITS_TOTAL_LIMIT) { //allow page load



        //start of page.php

            /**
             * The template for displaying pages
             *
             * This is the template that displays all pages by default.
             * Please note that this is the WordPress construct of pages and that
             * other "pages" on your WordPress site will use a different template.
             *
             * @package WordPress
             * @subpackage Twenty_Sixteen
             * @since Twenty Sixteen 1.0
             */

            get_header(); ?>
            <div id="primary" class="content-area">
                <main id="main" class="site-main" role="main">
                    <?php
                    // Start the loop.
                    while ( have_posts() ) : the_post();

                        // Include the page content template.
                        get_template_part( \'template-parts/content\', \'page\' );

                        // If comments are open or we have at least one comment, load up the comment template.
                        if ( comments_open() || get_comments_number() ) {
                            comments_template();
                        }

                        // End of the loop.
                    endwhile;
                    ?>

                </main><!-- .site-main -->

                <?php get_sidebar( \'content-bottom\' ); ?>

            </div><!-- .content-area -->

            <?php get_sidebar(); ?>
            <?php get_footer(); ?>

1 个回复
最合适的回答,由SO网友:jgraup 整理而成

首先,检查get_current_user_id. 然后,通过将信息添加到用户的元数据中,创建访问日志update_user_meta &;get_user_meta.

在您关心的每个页面上,您都可以运行一个函数来确定每个用户的使用率/限制。

如果他们违反了规则,您可以重定向到不包含该逻辑的页面wp_redirect.

相关推荐

数据库中缺少USERS表

我有一个功能正常的WordPress网站,有两个用户(一个管理员和一个编辑器)。我可以使用任意一个用户登录到WP后端。我可以创建页面等。但是当我查看phpMyAdmin时,数据库没有users 桌子然而,它确实有usermeta 表,显示管理员用户和编辑器用户的详细信息。如果执行如下SQL语句:SELECT * FROM "wp_users", 它确实显示了已创建的两个用户的两个条目,正如我希望看到的users 桌子(这两个条目之间唯一意想不到的区别是,管理员的user\\u acti