【问题标题】:Security script: make value lowercase and remove spaces before applying安全脚本:在应用前将值设为小写并删除空格
【发布时间】: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


【解决方案1】:

使用str_replace(' ', '', $string) 删除空格; trim($string) 删除输入周围的任何其他“垃圾”; strtolower($string) 获取小写字符串。比较字符串使用 strcmp($string, $string1) 最后你得到:

if (strcmp('serenity', strtolower(str_replace(' ', '', trim($_POST['user_proof'])))) == 0)

在您的代码中:

function add_register_field_validate($sanitized_user_login, $user_email, $errors) {
    if (empty($_POST['user_proof'])) {
        return $errors->add('proofempty', '<strong>ERROR</strong>: You did not answer the proof-of-humanship question.' );
    } elseif (strcmp('serenity', strtolower(str_replace(' ', '', trim($_POST['user_proof'])))) != 0) {
        return $errors->add('prooffail', '<strong>ERROR</strong>: You did not answer the proof-of-humanship question correctly.' );
    }
}

【讨论】:

  • 谢谢。您能否更新您的问题并告诉我我在 add_register_field_validate 函数中添加的确切位置?
【解决方案2】:
$user_proof = $_POST[ 'user_proof' ]; // this is the form field being posted.
$user_proof = strtolower($user_proof); // this will convert it to lower case
$user_proof = str_replace(" ", "", $user_proof); // this will remove spaces in string

EIDT:根据您的要求:

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.'  );
   } 

   $user_proof = $_POST[ 'user_proof' ]; // this is the form field being posted.
   $user_proof = strtolower($user_proof); // this will convert it to lower case
   $user_proof = str_replace(" ", "", $user_proof); // this will remove spaces in string

   if ( $user_proof != 'serenity' ) 
   {
    return $errors->add( 'prooffail', '<strong>ERROR</strong>: You did not answer the proof-of-humanship question correctly.'  );
   }
}

【讨论】:

  • 您能否更新您的问题并告诉我我在 add_register_field_validate 函数中添加的确切位置?
【解决方案3】:
if($input != 'serenity'){
    //Show errors
}

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(str_replace(' ', '', strip_tags(stripslashes($_POST['user_proof'])))) != 'serenity' ) {
        return $errors->add( 'prooffail', '<strong>ERROR</strong>: You did not answer the proof-of-humanship question correctly.'  );
    }
}

【讨论】:

  • 您能否更新您的问题并告诉我我在 add_register_field_validate 函数中添加的确切位置?
猜你喜欢
  • 1970-01-01
  • 2022-10-24
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多