阳光漫步 發表於 2023-12-14 00:00:00

wordpress无需插件实现拦截无中文留言的方法

<span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>Some Chinese Please插件可以拦截不带中文字的留言,本博客一直在用效果不错,不写入数据库,可有效地减少spam对服务器的无谓使用,其实可以将插件简化一下,直接用代码实现。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>将下面代码加到主题functions模板文件中,效果与使用插件相同。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><div style='margin: 8px 0px 1.1em; padding: 0px 0px 5px; outline: none; line-height: normal; border: 1px solid rgb(165, 165, 248); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-family: "Courier New", monospace; overflow: hidden; width: 686px; word-break: break-all;'>
<div style="margin: 0px; padding: 5px 5px 5px 10px; outline: none; line-height: 21.6px; font-weight: bold; background: rgb(244, 245, 255); border-bottom: 1px solid rgb(242, 251, 246); height: 15px;">
<span style="line-height: 14px; font-size: 14px; float: left; padding-left: 5px;">PHP Code</span><span style="line-height: 12px; font-weight: normal; float: right; cursor: pointer;">复制内容到剪贴板</span>
</div>
<div id="code_1776" style="margin: 0px; padding: 0px; outline: none; line-height: 21.6px;">
<ol style="margin: 0px 1px 0px 45px; padding: 2px 0px; outline: none; list-style-position: initial; list-style-image: initial; line-height: 25px; word-break: break-word; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-left: 1px solid rgb(221, 221, 221); color: rgb(92, 92, 92);">
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"><span style="line-height: 14px; color: green;">/* refused spam */</span><span style="line-height: 14px;">     </span></span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: blue; font-weight: bold;">function</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"> refused_spam_comments( </span><span style="line-height: 14px; font-size: 14px; color: rgb(221, 0, 0);">$comment_data</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"> ) {     </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(221, 0, 0);">$pattern</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"> = </span><span style="line-height: 14px; font-size: 14px; color: blue;">'/[一-龥]/u'</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">;     </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: blue; font-weight: bold;">if</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">(!preg_match(</span><span style="line-height: 14px; font-size: 14px; color: rgb(221, 0, 0);">$pattern</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">,</span><span style="line-height: 14px; font-size: 14px; color: rgb(221, 0, 0);">$comment_data</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">[</span><span style="line-height: 14px; font-size: 14px; color: blue;">'comment_content'</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">])) {     </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">wp_die(</span><span style="line-height: 14px; font-size: 14px; color: blue;">'评论必须含中文!'</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">);     </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">}     </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: blue; font-weight: bold;">return</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">( </span><span style="line-height: 14px; font-size: 14px; color: rgb(221, 0, 0);">$comment_data</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"> );     </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">}     </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">add_filter(</span><span style="line-height: 14px; font-size: 14px; color: blue;">'preprocess_comment'</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">,</span><span style="line-height: 14px; font-size: 14px; color: blue;">'refused_spam_comments'</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">);  </span>
</li>
</ol>
</div>
</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
<img title="wordpress无需插件实现拦截无中文留言的方法" alt="wordpress无需插件实现拦截无中文留言的方法" src="https://zhuji.jb51.net/uploads/img/202305/589d13b326359b4cb7aae51b546151f8.jpg" style="max-width:100%!important;height:auto!important;border: 1px solid rgb(204, 204, 204); vertical-align: middle; padding: 1px; overflow: hidden; max-width: 696px; width: 441px; height: 172px;"><br><br>
注:如果出现无效的情况,请将 <font color="#ff0000" style="line-height: 25.2px;">wp_die</font> 替换为 <font color="#ff0000" style="line-height: 25.2px;">err</font>,否则显示错误信息时,会造成模版错位,其它使用ajax评论的主题也可参照修改。<br>
其它一些写入数据库的防spam插件不建议使用, 比如WordPress官方的防垃圾插件Akismet会在数据库中留下大量的信息,增加服务器负担。<br>
针对那些使用技术手段,比如GET、POST等方式不填写前台表单,直接读取后台程序文件的spam,只有屏蔽IP才能缓解疯狗一样的攻势,其它什么验证码、滑动解锁等等都没用。<br>
可以在.htaccess文件中添加:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode7" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
Order Deny,Allow<br>
Deny from xxx.xxx.xxx.xx<br>
Deny from xxx.xxx.xxx.xx</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
禁止某些IP访问。<br>
本文来源知更鸟</p>
頁: [1]
查看完整版本: wordpress无需插件实现拦截无中文留言的方法