ColdFusion recaptcha表单和服务器验证

时间:2017-02-21 18:03:35

标签: coldfusion recaptcha

我基本上想要进行服务器端验证,然后将表单数据发布到URL。我的Google Foo缺乏,因为我没有找到任何实际的例子。

这是我到目前为止所做的,但也许我错了。

<!doctype html>
<html lang="en">
<head>
    <title>Untitled</title>
<script src="https://www.google.com/recaptcha/api.js" type="text/javascript"></script>
<script type="text/javascript">
    var noRobot = function(response){
            document.getElementById("sub").disabled = false;
    }
</script>
</head>
<body>
    <!-- Form Post -->
    <CFIF CGI.Request_method IS "post">
        <!-- Sends the recaptcha token to Google to verify -->
        <cfinvoke component="/components/recaptcha" method="isRecaptchaGood" returnvariable="isGood">
        <!-- Only post data if valid recapthca -->
        <CFIF isGood>
            <p>Good Recaptcha</p>
            <!--- 
                How do I foward on all the form data to the API?  
                // <cfhttpparam type="CGI" value="cgivar " name="mycgi">
                // <cfhttp method="Post" url="http://myapi.com/api/users">
            --->
            <CFEXIT>
        <CFELSE>
            <p>Bad Recaptcha</p>
        </CFIF>
    </CFIF>
    <CFOUTPUT>
        <!---
            I used to just post the data to the API, but now I first need to validate the recaptcha response on the server
            <form action="http://myapi.com/api/users" method="post">
        --->
        <form action="#CGI.Script_Name#" method="post">
            Name: <input type="text" name = "user" />
        <CFIF Application.recaptcha.enabled>
            <!--- A simple wrapper around generating the recaptcha div with the appropriate key --->
            <cfinvoke component="/components/recaptcha" method="makeRecaptcha">
        </CFIF>
        <button type="submit" id="sub" name="con" disabled="disabled">Continue</button>
        </form>
    </CFOUTPUT>
</body>
</html>

作为免责声明,我有大约6个小时的ColdFusion经验,因此我认为我遗漏了一些简单的事情。

表格值编辑:

<body>
    <cfparam name="form.user" type="string" default="">
    <cfparam name="form.amount" type="string" default="">
    <form action="#CGI.Script_Name#" method="post">
        User: <input type="text" name="user" value="#form.user#" />
        Amount: <input type="number" name="amount" value="#form.amount#" />
    </form>
</body>

1 个答案:

答案 0 :(得分:2)

感谢@ dan-bracuk和@Leigh的帮助,这里的工作最终结果可能会帮助其他人:

<!doctype html>
<html lang="en">
<head>
    <title>Untitled</title>
<script src="https://www.google.com/recaptcha/api.js" type="text/javascript"></script>
<script type="text/javascript">
    <!--- The callback function recaptcha calls when the user has completed the step--->
    <!--- Enable the Submit button--->
    var recaptchaResponse = function(response){
        document.getElementById("submit").disabled = false;
    }
</script>
</head>
<body>
    <!--- Declare default values for the form so if it fails validation the form can be redisplayed --->
    <cfparam name="form.user" type="string" default="">
    <cfparam name="form.amount" type="string" default="">
    <!--- On Form Post --->
    <CFIF CGI.Request_method IS "post">
        <!--- Sends the recaptcha token to Google to verify --->
        <cfinvoke component="components/recaptcha" method="isRecaptchaGood" returnvariable="isValid">
        <!--- Only post data if valid recapthca --->
        <CFIF isValid>
            <p>Good Recaptcha</p>
            <!--- Post the to API --->
             <cfhttp method="Post" url="http://localhost:1249/api/users">
                <!--- Send Form field values except for the recaptcha response --->
                <cfloop list="#Form.FieldNames#" item="i"> 
                    <CFIF form[i] eq "G-RECAPTCHA-RESPONSE">
                        <cfcontinue />
                    <CFELSE>
                        <cfhttpparam type="formfield" name="#i#" value="#form[i]#">
                    </CFIF>
                </cfloop>
             </cfhttp>
             <cflocation url="next.cfm">
            <CFEXIT>
        <CFELSE>
            <!--- Recaptcha verification failed, redisplay the Form --->
            <p>Bad Recaptcha</p>
        </CFIF>
    </CFIF>
    <CFOUTPUT>
        <!--- Post the Form back to itself first for server side validation --->
        <form action="#CGI.Script_Name#" method="post">
            User: <input type="text" name="user" value="#form.user#" />
            Amount: <input type="number" name="amount" value="#form.amount#" />
            <CFIF Application.recaptcha.enabled>
                <!--- A simple wrapper around generating the recaptcha div with the appropriate key --->
                <cfinvoke component="components/recaptcha" method="makeRecaptcha">
            </CFIF>
            <!--- Initially disable the continue button until the user completes the Recaptcha --->
            <button type="submit" id="submit" disabled="disabled">Continue</button>
        </form>
    </CFOUTPUT>
</body>
</html>