簡體   English   中英

自定義驗證碼提交表單,即使代碼不正確

[英]custom captcha submits form even if code is not correct

我使用的是自定義驗證碼,但即使代碼不正確,也正在提交表單,這是索引頁面的主要代碼

$(document).ready(function() { 

 $('#Send').click(function() {  

        // name validation

        var nameVal = $("#name").val();
        if(nameVal == '') {

            $("#name_error").html('');
            $("#name").after('<label class="error" id="name_error">Please enter your name.</label>');
            return false
        }
        else
        {
            $("#name_error").html('');
        }

        /// email validation

        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        var emailaddressVal = $("#email").val();

        if(emailaddressVal == '') {
            $("#email_error").html('');
            $("#email").after('<label class="error" id="email_error">Please enter your email address.</label>');
            return false
        }
        else if(!emailReg.test(emailaddressVal)) {
            $("#email_error").html('');
            $("#email").after('<label class="error" id="email_error">Enter a valid email address.</label>');
            return false

        }
        else
        {
            $("#email_error").html('');
        }

        $.post("post.php?"+$("#MYFORM").serialize(), {

        }, function(response){

        if(response==1)
        {
            $("#after_submit").html('');
            $("#Send").after('<label class="success" id="after_submit">Your message has been submitted.</label>');
            change_captcha();
            clear_form();
        }
        else
        {
            $("#after_submit").html('');
            $("#Send").after('<label class="error" id="after_submit">Error ! invalid captcha code .</label>');
        }


    });

    return false;
 });

 // refresh captcha
 $('img#refresh').click(function() {  

        change_captcha();
 });

 function change_captcha()
 {
    document.getElementById('captcha').src="get_captcha.php?rnd=" + Math.random();
 }

 function clear_form()
 {
    $("#name").val('');
    $("#email").val('');
    $("#message").val('');
 }
}); 

index.php代碼(session_start位於文件的開頭)

<form action="#" name="MYFORM" id="MYFORM">
<label>Name</label>
<input name="name" size="30" type="text" id="name">
<br clear="all" />
<label>Email</label>
<input name="email" size="30" type="text" id="email">
<br clear="all" />
<label>Message</label>
<textarea id="message" name="message"></textarea>
<br clear="all" />  
<div id="wrap" align="center">
    <img src="get_captcha.php" alt="" id="captcha" />       
    <br clear="all" />
    <input name="code" type="text" id="code">
</div>
<img src="refresh.jpg" width="25" alt="" id="refresh" />        
<br clear="all" /><br clear="all" />
<label>&nbsp;</label>
<input value="Send" type="submit" id="Send">
</form> 

post.php代碼

session_start();    
if(@$_REQUEST['code'] || @strtolower($_REQUEST['code']) == strtolower($_SESSION['random_number']))
{               
    echo 1;// submitted         
}
else
{
    echo 0; // invalid code
}

get_captcha.php文件

session_start();
$string = '';
for ($i = 0; $i < 5; $i++) {
$string .= chr(rand(97, 122));
}
$_SESSION['random_number'] = $string;
$dir = 'fonts/';
$image = imagecreatetruecolor(165, 50);
$num = rand(1,2);
if($num==1)
{
$font = "Capture it 2.ttf"; // font style
}
else
{
$font = "Molot.otf";// font style
}
$num2 = rand(1,2);
if($num2==1)
{
$color = imagecolorallocate($image, 113, 193, 217);// color
}
else
{
$color = imagecolorallocate($image, 163, 197, 82);// color
}

$white = imagecolorallocate($image, 255, 255, 255); // background color white
imagefilledrectangle($image,0,0,399,99,$white);

imagettftext ($image, 30, 0, 10, 40, $color, $dir.$font, $_SESSION['random_number']);

header("Content-type: image/png");
imagepng($image);

這是整個代碼http://download1473.mediafire.com/ef2uaexp2hmg/3j63qbbq7xiwryi/captcha.rar

(以其他答案為基礎)

您犯了一個錯誤。 為什么用@符號抑制錯誤? 那只是不好的編碼。 您可能在某個地方遇到了更大的問題,而您對此一無所知。 如果這是一種習慣,那么當您處理關鍵數據時該怎么辦? 您要忽略錯誤嗎?

對於您的實際代碼,是否要在if語句中首先使用isset() 那是你的意思嗎?

這就是您的代碼應該是什么樣子...

session_start();    
if(isset($_REQUEST['code']) && strtolower($_REQUEST['code']) == strtolower($_SESSION['random_number']))
{               
    echo 1;// submitted         
}
else
{
    echo 0; // invalid code
}

post.php代碼中可能應該有一個&&,否則,只要$ _REQUEST ['code']不為空,它將執行echo 1。

session_start();    
if(@$_REQUEST['code'] && @strtolower($_REQUEST['code']) == strtolower($_SESSION['random_number']))
{               
    echo 1;// submitted         
}
else
{
    echo 0; // invalid code
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM