子比主题防止搜索页被恶意留痕解决方案-免更新修改版本

方法来自李初一,我已实现不更改主题文件,使用钩子替换函数,实现更新主题免修改。初一让我发出来,那就在本贴发一下。

发布时间2024年11月1日,主题版本8.0,后续是否可用,自测

修改帖子时间2025年10月6日中秋节,主题版本8.2,有些修改,所以代码更新一下

不过这个函数以及文件,老唐很久没动过了,应该可以适用几个版本,刚说完就动了?仿佛就在昨日

原修改教程贴:https://www.zibll.com/forum-post/28383.html(内容已经失效)

防止恶意人员,借用你网站的搜索功能,在搜索引擎留下痕迹

注意,可能会被对方狗急跳墙攻击你的网站,所以要配合robot.txt,waf防火墙,共同进行防止留痕和攻击。

robot.txt请添加Disallow:/?s=*,防止痕迹被搜索引擎记录,导致站点被k排名

waf使用正则模式,以人机验证方式防御搜索以及登录,防止被对方攻击,导致php任务创建过多,站点崩溃

宝塔nginx防火墙,url增强模式,方法跳转验证,正则表达式为以下

url链接^/,参数s,防御搜索接口

url链接^/user_sign,参数tab,防御登录接口

说明

仅允许认证用户查看,也就是正版用户,请不要转载到其他任何地方,仅允许查看使用,我个人很讨厌那种转载不留名,不留链接的shit,所以一刀切,不允许转发到任何地方。不要触犯底线,谢谢。因为这个这种事,我已经在子比论坛删过一次帖子了,有些是老唐加精的教程贴。所以你们不要搞盗内容的这种事。

如果连正版用户,都做不到这些要求,那以后为没必要在这里发教程了。我都想不通,版权意识这么差,你怎么会买正版主题?

前人栽树,后人乘凉,希望你不要把别人的研究成果当成自己的作品。

教程

在主题根目录创建func.php文件,填入<?php,换行填入以下代码(如果有,当我没说)

//搜索基础功能来自子比主题作者老唐
//搜索功能规避风险来自子比主题用户李初一
//二次开发优化来自子比用户天无神话

//替换搜索页面文件
function wxs_search_page_template($template) {
    // 判断是否是搜索页,这里假设当查询变量's'存在且有值时为搜索页
    if (isset($_GET['s']) &&!empty($_GET['s'])) {
        $new_template = get_stylesheet_directory(). '/wxs_new_search.php';
        if (file_exists($new_template)) {
            return $new_template;
        }
    }

    return $template;
}
add_filter('template_include', 'wxs_search_page_template');

//搜索搜索
//修改head注册zib_seo
remove_action('wp_head', 'zib_seo', 1);
add_action('wp_head', 'wxs_zib_seo', 1);
function wxs_zib_seo()
{
    if (_pz('post_keywords_description_s')) {
        zib_title();
        //调用修改后的函数
        wxs_zib_keywords();
        //wxs_zib_keywords();
        wxs_zib_description();
        //wxs_zib_description();
        if (!is_singular()) {
            echo '<link rel="canonical" href="' . esc_url(zib_get_current_url()) . '" />';
        } else {
            rel_canonical();
        }

    } elseif (apply_filters('echo_seo_title', false)) {
        zib_title();
    }
}
//关键字
function wxs_zib_keywords($echo = true)
{
    global $new_keywords;
    if ($new_keywords) {
        if ($echo) {
            echo "<meta name=\"keywords\" content=\"{$new_keywords}\">\n";
            return;
        } else {
            return $new_keywords;
        }
    }

    global $s, $post;
    $keywords = '';

    if (is_singular()) {
        $the = trim(zib_get_post_meta($post->ID, 'keywords', true));
        if ($the) {
            $keywords = $the;
        } else {
            if ('post' === $post->post_type) {
                foreach (array('post_tag', 'category', 'topic') as $taxonomy) {
                    $the_terms = get_the_terms($post->ID, $taxonomy);
                    if ($the_terms) {
                        foreach ($the_terms as $term) {
                            if (isset($term->name) && trim($term->name)) {
                                $keywords .= trim($term->name) . ',';
                            }
                        }
                    }
                }
            } elseif ('forum_post' === $post->post_type) {
                foreach (array('forum_tag', 'forum_topic') as $taxonomy) {
                    $the_terms = get_the_terms($post->ID, $taxonomy);
                    if ($the_terms) {
                        foreach ($the_terms as $term) {
                            if (isset($term->name) && trim($term->name)) {
                                $keywords .= trim($term->name) . ',';
                            }
                        }
                    }
                }
            } elseif ('plate' === $post->post_type) {
                foreach (array('plate_cat') as $taxonomy) {
                    $the_terms = get_the_terms($post->ID, $taxonomy);
                    if ($the_terms) {
                        foreach ($the_terms as $term) {
                            if (isset($term->name) && trim($term->name)) {
                                $keywords .= trim($term->name) . ',';
                            }
                        }
                    }
                }
                $keywords .= trim($post->post_title) . ',';
            }elseif ('shop_product' === $post->post_type) {
                foreach (array('shop_cat', 'shop_tag') as $taxonomy) {
                    $the_terms = get_the_terms($post->ID, $taxonomy);
                    if ($the_terms) {
                        foreach ($the_terms as $term) {
                            if (isset($term->name) && trim($term->name)) {
                                $keywords .= trim($term->name) . ',';
                            }
                        }
                    }
                }
            } else {
                $keywords = trim(wp_title('', false));
            }
        }
    } elseif (is_home()) {
        $keywords = _pz('keywords');
    } elseif (is_search()) {
        //修改关键词
        $keywords = esc_html( '搜索');
        //$keywords = esc_html($s, 1);
    } elseif (is_tax()) {
        $object_id = get_queried_object_id();
        $keywords  = _get_tax_meta($object_id, 'keywords');
        if (!$keywords) {
            $keywords = single_term_title('', false);
        }
    } else {
        $keywords = wp_title('', false);
    }

    $keywords = str_replace(',',',',$keywords);
    $keywords = rtrim(trim($keywords), ',');

    if ($keywords) {
        if ($echo) {
            echo "<meta name=\"keywords\" content=\"{$keywords}\">\n";
        } else {
            return $keywords;
        }
        return;
    }
}

//网站描述
function wxs_zib_description($echo = true)
{
    global $new_description;
    if ($new_description) {
        if ($echo) {
            echo "<meta name=\"description\" content=\"$new_description\">\n";
            return;
        } else {
            return $new_description;
        }
    }

    global $s, $post;
    $description = '';
    $blog_name   = get_bloginfo('name');
    if (is_singular()) {

        $description = trim(zib_get_post_meta($post->ID, 'description', true));

        if (!$description) {
            $description = zib_get_excerpt(210, '...', $post);
            $description = mb_substr($description, 0, 200, 'utf-8');
        }

        if (!$description) {
            $description = $blog_name . '-' . trim(wp_title('', false));
        }

    } elseif (is_home()) {
        $description = _pz('description');
    } elseif (is_tax()) {
        global $wp_query;
        $cat_ID      = get_queried_object_id();
        $description = _get_tax_meta($cat_ID, 'description');
        if (!$description) {
            $description = trim(strip_tags(term_description()));
        }

        if (!$description) {
            $description = single_term_title('', false) . zib_get_delimiter_blog_name();
        }

    } elseif (is_archive()) {
        $description = $blog_name . "'" . trim(wp_title('', false)) . "'";
    } elseif (is_search()) {
        //修改去除搜索内容
        $description = $blog_name . "的搜索結果";
        //$description = $blog_name . ": '" . esc_html($s, 1) . "' 的搜索結果";
    } else {
        $description = $blog_name . "'" . trim(wp_title('', false)) . "'";
    }
    $description = esc_attr($description);
    if ($echo) {
        echo "<meta name=\"description\" content=\"$description\">\n";
    } else {
        return $description;
    }
    return;
}

在主题根目录创建wxs_new_search.php文件

文件内填入以下代码

<?php
/*
 * @Author: Qinver
 * @Url: zibll.com
 * @Date: 2021-04-11 21:36:20
 * @LastEditTime : 2025-07-07 14:38:26
 */

// 获取访问者的User-Agent信息
//$userAgent = $_SERVER['HTTP_USER_AGENT'];

// 定义要拒绝的搜索引擎爬虫的User-Agent关键词数组
//$blockedBots = array(
//    'Baiduspider',
//    'googlebot',
//    'bing',
//    'sogou',
//    'yahoo',
//    'Bytespider',
//    'YisouSpider',
//    '360spider',
//    'sosospider',
//    'msnbot',
//    'YandexBot',
//    'YodaoBot',
//    'DNSPod-Monitor',
//    'AspiegelBot'
//);

// 循环检查是否匹配要拒绝的爬虫
//foreach ($blockedBots as $bot) {
//    if (stripos($userAgent, $bot)!== false) {
        // 如果匹配到了,返回403 Forbidden状态码并终止脚本执行,拒绝访问
//        header('HTTP/1.1 403 Forbidden');
//        exit;
//    }
//}



$s    = trim(esc_attr(strip_tags($s)));
$cat  = !empty($_REQUEST['trem']) ? trim(strip_tags($_REQUEST['trem'])) : '';
$type = $search_type;
$user = !empty($_REQUEST['user']) ? (int) $_REQUEST['user'] : '';

$search_types     = zib_get_search_types();
$search_type_name = $search_types[$type];

//不拼接搜索内容
$new_title = '搜索' . $search_type_name;
//$new_title = $s . ' ' . '搜索' . $search_type_name;
if ($paged > 1) {
    $new_title .= _get_delimiter() . '第' . $paged . '页';
}
$new_title .= zib_get_delimiter_blog_name();

//保存搜索历史
zib_save_history_search($s . ($type ? '&type=' . $type : ''));

if ($type != 'user' && $wp_query->have_posts() && $paged == 1) {
    zib_update_search_keywords($s . ($type ? '&type=' . $type : ''));
}

get_header();
?>
<?php if (function_exists('dynamic_sidebar')) {
    echo '<div class="container fluid-widget">';
    dynamic_sidebar('all_top_fluid');
    dynamic_sidebar('search_top_fluid');
    echo '</div>';
}

?>
<main class="container">
    <div class="content-wrap">
        <div class="content-layout">
            <?php if (function_exists('dynamic_sidebar')) {
                dynamic_sidebar('search_top_content');
            }
            ?>

            <?php
            $args = array(
                'class'      => 'main-search',
                'show_posts' => false,
                'in_cat'     => $cat,
                'in_type'    => $type,
                'in_user'    => $user,
                's'          => $s,
            );
            echo '<div class="zib-widget"' . ($paged < 2 ? ' win-ajax-replace="search"' : '') . '>';
            zib_get_main_search($args, true);
            echo '</div>';
            ?>
            <?php
            zib_search_content();
            ?>
            <?php if (function_exists('dynamic_sidebar')) {
                dynamic_sidebar('search_bottom_content');
            }
            ?>
        </div>
    </div>
    <?php get_sidebar(); ?>
</main>
<?php if (function_exists('dynamic_sidebar')) {
    echo '<div class="container fluid-widget">';
    dynamic_sidebar('search_bottom_fluid');
    dynamic_sidebar('all_bottom_fluid');
    echo '</div>';
}
?>
<?php get_footer();

转载 https://www.zibll.com/forum-post/30560.html

© 版权声明
THE END
文章不错?点个赞呗
点赞15
评论 抢沙发

请登录后发表评论

    暂无评论内容