【发布时间】:2014-09-25 13:16:11
【问题描述】:
在我的注册页面上,我有一个用户必须回答的安全问题。它运行良好,并且阻止了垃圾邮件机器人。
这是脚本(非常轻量级):
add_action( 'register_form', 'add_register_field' );
function add_register_field() { ?>
<p>
<label><?php _e('What is the name of the ship in the TV show Firefly?') ?><br />
<input type="text" name="user_proof" id="user_proof" class="input" size="25" tabindex="20" /></label>
</p>
<?php }
add_action( 'register_post', 'add_register_field_validate', 10, 3 );
function add_register_field_validate( $sanitized_user_login, $user_email, $errors) {
if (!isset($_POST[ 'user_proof' ]) || empty($_POST[ 'user_proof' ])) {
return $errors->add( 'proofempty', '<strong>ERROR</strong>: You did not answer the proof-of-humanship question.' );
} elseif ( strtolower( $_POST[ 'user_proof' ] ) != 'serenity' ) {
return $errors->add( 'prooffail', '<strong>ERROR</strong>: You did not answer the proof-of-humanship question correctly.' );
}
}
下面是它的外观图片:
所以用户必须输入的答案是serenity。问题是有些用户将其输入为Serenity,或者在某处添加了一个随机空格。
我的问题是,我怎样才能使他们输入的值转换为小写字母,并在检查值是否正确之前删除所有空格?这样,如果他们输入Ser enItY,就可以了。
【问题讨论】:
-
strtolower() 将使字符串变为小写。 str_replace() 或一些正则表达式将帮助您删除字符串中的空格。 trim() 还可以帮助您删除字符串左右的空格。由于您标记了 php 这个问题,我假设您想要服务器端功能,而不是任何 js 代码来做同样的事情。
-
@andrew 绝对不是服务器端的。你能用你的建议写一个答案吗?如果有,请参考我的代码。
-
它也会阻止一些用户......我不知道这个问题的答案,直到我刚才用谷歌搜索了它
标签: php security registration spam-prevention