WordPress如何在顯示結果前在搜索頁面添加人機驗證碼?
WordPress 搜索一直是一個內存密集型功能。 如果你有很多文章,那麼執行搜索會比較慢,那麼我們怎樣才能儘可能地防止它呢? 比如機器人掃描搜索頁面,可能會直接導致mysql進程在內存滿時被終止。
這裡我們可以添加一個搜索驗證碼,用戶第一次搜索時需要進行簡單的人機驗證。 這樣一方面可以有效防止惡意掃描導致的內存損壞,另一方面可以防止惡意請求關鍵字生成結果頁面。
模板兔寫一個簡單的驗證機制,可以在主題的functions.php中加入如下代碼。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/* Plugin Name: 搜索驗證碼 Plugin URI: https://www.mobantu.com Description: WordPress 搜索驗證碼 Version: 1.0 Author: <span class="wp_keywordlink">模板兔</span> Author URI: https://www.mobantu.com */ function esc_search_captcha( $query, $error = true ) { if (is_search() && !is_admin()) { if(!isset($_COOKIE['esc_search_captcha'])){ $query->is_search = false; $query->query_vars['s'] = false; $query->query['s'] = false; if ( $error == true ){ //$query->is_404 = true; if(isset($_POST['result'])){ if($_POST['result'] == $_COOKIE['result']){ $_COOKIE['esc_search_captcha'] = 1; setcookie('esc_search_captcha',1,0,'/'); echo '<script>location.reload();</script>'; } } $num1 = rand(1,50); $num2 = rand(1,50); $result = $num1+$num2; $_COOKIE['result'] = $result; setcookie('result',urldecode($result),0,'/'); ?> <html> <head> <meta charset="UTF-8"> <title>人機驗證</title> <style> body{color: #333;text-align: center;font-size: 16px;} .erphp-search-captcha{margin: 50px auto 15px;max-width: 250px;width: 100%;padding: 40px 20px;border: 1px solid #ddd;text-align: center;border-radius: 5px;} .erphp-search-captcha form{margin: 0} .erphp-search-captcha input{border: none;border-bottom: 1px solid #666;width: 50px;text-align: center;font-size: 16px;} .erphp-search-captcha input:focus{outline: none;} .erphp-search-captcha button{border: none;background: transparent;color: #ff5f33;cursor: pointer;} .erphp-search-captcha button:focus{outline: none;} a{color: #000;font-size: 12px;} </style> </head> <body> <div class="erphp-search-captcha"> <form action="" method="post"><?php echo $num1;?> + <?php echo $num2;?> = <input type="text" name="result" required /> <button type="submit">驗證</button></form> </div> <a href="https://www.mobantu.com/<?php echo home_url();?>">返回首頁</a> </body> </html> <?php exit; } } } } add_action( 'parse_query', 'esc_search_captcha' ); |
由於網站搬家,部分鏈接失效,如無法下載,請聯繫站長!謝謝支持!
1. 帶 [親測] 說明源碼已經被站長親測過!
2. 下載後的源碼請在24小時內刪除,僅供學慣用途!
3. 分享目的僅供大家學習和交流,請不要用於商業用途!
4. 本站資源售價只是贊助,收取費用僅維持本站的日常運營所需!
5. 本站所有資源來源於站長上傳和網路,如有侵權請郵件聯繫站長!
6. 沒帶 [親測] 代表站長時間緊促,站長會保持每天更新 [親測] 源碼 !
7. 盜版ripro用戶購買ripro美化無擔保,若設置不成功/不生效我們不支持退款!
8. 本站提供的源碼、模板、插件等等其他資源,都不包含技術服務請大家諒解!
9. 如果你也有好源碼或者教程,可以到審核區發布,分享有金幣獎勵和額外收入!
10.如果您購買了某個產品,而我們還沒來得及更新,請聯繫站長或留言催更,謝謝理解 !
GG資源網 » WordPress如何在顯示結果前在搜索頁面添加人機驗證碼?
1. 帶 [親測] 說明源碼已經被站長親測過!
2. 下載後的源碼請在24小時內刪除,僅供學慣用途!
3. 分享目的僅供大家學習和交流,請不要用於商業用途!
4. 本站資源售價只是贊助,收取費用僅維持本站的日常運營所需!
5. 本站所有資源來源於站長上傳和網路,如有侵權請郵件聯繫站長!
6. 沒帶 [親測] 代表站長時間緊促,站長會保持每天更新 [親測] 源碼 !
7. 盜版ripro用戶購買ripro美化無擔保,若設置不成功/不生效我們不支持退款!
8. 本站提供的源碼、模板、插件等等其他資源,都不包含技術服務請大家諒解!
9. 如果你也有好源碼或者教程,可以到審核區發布,分享有金幣獎勵和額外收入!
10.如果您購買了某個產品,而我們還沒來得及更新,請聯繫站長或留言催更,謝謝理解 !
GG資源網 » WordPress如何在顯示結果前在搜索頁面添加人機驗證碼?