2014-09-25 3 views
0

등록 페이지에서 사용자가 대답해야하는 보안 질문이 있습니다. 그것은 잘 작동하고 스팸 봇을 중지했습니다. 여기 보안 스크립트 : 값을 소문자로 만들고 적용 전에 공백을 제거하십시오.

는 (매우 가벼운) 스크립트입니다 :

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

그리고 여기 보이는 방법의 사진입니다 :

enter image description here

그래서 입력에 사용자가 가지고있는 대답은 serenity됩니다. 문제는 일부 사용자가 Serenity으로 입력하거나 임의의 빈 공간을 어딘가에 추가하는 것입니다. 그래서

은 어떻게 값들이 입력 소문자로 변환됩니다 및 공백이 값이 정확한지 확인 이전을 제거 그것을 내 질문을 할 수 있습니까? 이렇게하면 Ser enItY을 입력하면 문제가되지 않습니다.

+0

strtolower()가 st를 생성합니다. 반지를 소문자로 바꾼다. str_replace() 또는 일부 정규 표현식을 사용하면 string의 공백을 제거하는 데 도움이됩니다. trim()은 문자열의 왼쪽과 오른쪽 공백을 제거하는데도 도움이됩니다. PHP에서이 질문에 태그를 지정 했으므로, 서버 측 함수를 원한다고 가정하고, js 코드가 같은 일을하지 않을 것이라고 가정합니다. – andrew

+0

@andrew 서버 측은 틀림 없습니다. 제안 사항에 대한 답을 적어주십시오. 그렇게한다면 내 코드를 참조로 사용하십시오. –

+0

그것은 몇 명의 사용자를 막을 것입니다 .... 나는 그 질문에 대한 답을 알지 못했고, 나는 그 순간을봤을 때까지 –

답변

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

고맙습니다. 질문을 업데이트하고 add_register_field_validate 함수에서 정확히 어디에 추가했는지 보여줄 수 있습니까? –

+0

@HenrikPetterson 코드로 업데이트되었습니다. – Justinas

+0

대단히 감사합니다! –

1
$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.' ); 
    } 
} 
+0

질문을 업데이트하고 add_register_field_validate 함수에서 정확히 어디에 추가했는지 보여줄 수 있습니까? –

+0

@HenrikPetterson 내 편집을 확인하십시오 – andrew

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

질문을 업데이트하고 add_register_field_validate 함수에 추가 할 위치를 정확하게 표시해 주시겠습니까? 귀하의 요청에 따라 –

+0

업데이트 됨 – ksealey