ASP Classic CAPTCHA Request Dynamic Settings Code Example

The ASP Classic Captcha request dynamic settings code example shows how to dynamically adjust BotDetect CAPTCHA configuration, potentially on each Http request made by the client.

First Time Here?

Check the BotDetect Developer Crash Course for key integration steps.

Multiple ASP forms within the same ASP website can be protected by BotDetect Captcha challenges: e.g. you could include BotDetect.asp in both your Contact form and Registration form source.

Any ASP code setting Captcha properties in the CaptchaConfig.asp file will be executed not only for each protected form GET or POST request (like Captcha configuration code placed in form source would be), but also for each each GET request loading a Captcha image or sound, or making an Ajax Captcha validation call.

If configured values are dynamic (e.g. BD randomization or other function calls in CaptchaConfig.asp code), they will be re-calculated for each Captcha challenge generated. For example, Captcha ImageStyle randomized in CaptchaConfig.asp code will change on each Captcha reload button click.

This means your code can reliably keep track of visitor interaction with the Captcha challenge and dynamically adjust its settings. Also, while CaptchaConfig.asp settings apply to all Captcha instances by default, you can also selectively apply them based on CaptchaId.

To show an example of the possible dynamic Captcha configuration adjustments, this code example increases the difficulty of the Captcha test if the visitor associated with the current PHP Session fails a certain number of Captcha validation attempts, and also sets the Captcha locale to Chinese for requests from a certain IP range.

Installed Location

By default, the Classic ASP request dynamic settings code example is installed at:
C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp\WebApp\CaptchaRequestDynamicSettingsExample

You can also run it from the BotDetect Start Menu:
Programs > Captcha Inc > BotDetect 4 CAPTCHA Component > ASP > Web Applications > Run

Download the BotDetect Classic ASP CAPTCHA Component and run this example

Default.asp

<!-- #include file ="BotDetect.asp" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>BotDetect ASP Classic CAPTCHA Options: Request Dynamic Settings Code 
  Example</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <link type="text/css" rel="Stylesheet" href="<%= CaptchaUrls.LayoutStylesheetUrl() %>" />
  <link type="text/css" rel="Stylesheet" href="StyleSheet.css" />
</head>
<body>
  <form method="post" action="" class="column" id="form1">

    <h1>BotDetect ASP Classic CAPTCHA Options: 
    <br /> Request Dynamic Settings Code Example</h1>
    
    <fieldset>
        <legend>ASP Classic CAPTCHA validation</legend>
        <label for="CaptchaCode">Retype the characters from the picture:</label>
        
        <% ' Adding BotDetect CAPTCHA to the page 
          Dim DynamicCaptcha : Set DynamicCaptcha = (New Captcha)(
          "DynamicCaptcha")DynamicCaptcha.UserInputID = "CaptchaCode"
          Response.Write DynamicCaptcha.Html %>
        
        <div class="validationDiv">
            <input name="CaptchaCode" type="text" id="CaptchaCode" />
            <input type="submit" name="ValidateCaptchaButton" value="Validate" 
            id="ValidateCaptchaButton" />
            
            <% ' CAPTCHA user input validation (only if the form was sumbitted)
              If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
                Dim isHuman : isHuman = DynamicCaptcha.Validate()
                If Not isHuman Then 
                  ' CAPTCHA validation failed, show error message
                  Response.Write "<span class=""incorrect"">Incorrect code</span> "
                  
                  ' increment failed validations count
                  Dim count : count = 0
                  Dim temp : temp = Session("FailedValidationsCount")
                  If (Not IsEmpty(temp) And IsNumeric(temp)) Then
                    count = CInt(temp)
                  End If
                  count = count + 1
                  Session("FailedValidationsCount") = count
                Else 
                  ' CAPTCHA validation passed, perform protected action
                  Response.Write "<span class=""correct"">Correct code</span>"
                  
                  ' reset failed validations count
                  Session("FailedValidationsCount") = Empty
                End If 
              End If
            %>
        </div>
    </fieldset>
    
    <div id="output">
    <%
      Dim failed : failed = 0
      Dim saved : saved = Session("FailedValidationsCount")
      If (Not IsEmpty(saved) And IsNumeric(saved)) Then
        failed = CInt(saved)
      End If
      Response.Write "<p>Failed Captcha validations: " & failed & "</p>"
      If (failed < 3) Then
        Response.Write "<p>Dynamic Captcha difficulty: Easy</p>"
      ElseIf (failed < 10) Then
        Response.Write "<p>Dynamic Captcha difficulty: Moderate</p>"
      Else
        Response.Write "<p>Dynamic Captcha difficulty: Hard</p>"
      End If
    %>
    </div>
  </form>
</body>
</html>

CaptchaConfig.asp

<%

' BotDetect ASP Captcha configuration options
' ---------------------------------------------------------------------------

' BotDetect.asp settings are usually global and apply to all Captcha instances in
' the application; if some settings need to be apply only to a particular Captcha 
' instance, this is how settings can be conditionally applied based on CaptchaId
If (0 = StrComp(BDC_CurrentCaptchaId, "DynamicCaptcha", vbTextCompare)) Then
  BotDetect.SoundEnabled = False
End If


' re-calculated on each image request
Dim imageStyleNames(2)
imageStyleNames(0) = "Graffiti"
imageStyleNames(1) = "SunAndWarmAir"
imageStyleNames(2) = "Overlap"
BotDetect.ImageStyle = BDC_RandomImageStyleFrom(imageStyleNames)


' dynamic Captcha settings depending on failed validation attempts: increase Captcha 
' difficulty according to number of previously failed validations
Dim count : count = 0
Dim temp : temp = Session("FailedValidationsCount")
If (Not IsEmpty(temp) And IsNumeric(temp)) Then
  count = CInt(temp)
End If
If (count < 3) Then
  BotDetect.CodeLength = BDC_RandomFromRange(3, 4)
  BotDetect.CodeStyle = BDC_CodeStyles("Numeric")
  BotDetect.CodeTimeout = 600 ' 10 minutes
ElseIf (count < 10) Then
  BotDetect.CodeLength = BDC_RandomFromRange(4, 6)
  BotDetect.CodeStyle = BDC_CodeStyles("Alpha")
  BotDetect.CodeTimeout = 180 ' 3 minutes
Else
  BotDetect.CodeLength = BDC_RandomFromRange(6, 9)
  BotDetect.CodeStyle = BDC_CodeStyles("Alphanumeric")
  BotDetect.CodeTimeout = 60 ' 1 minute
End If


' set Captcha locale to Chinese for requests from a certain IP range
Dim TestIpRange : TestIpRange = "223.254."
Dim userIPAddress : userIPAddress = Request.ServerVariables("REMOTE_ADDR")
If (userIPAddress <> "" And InStr(1, userIPAddress, TestIpRange, 1) = 1) Then
  BotDetect.CodeStyle = BDC_CodeStyles("Alpha")
  BotDetect.Locale = "cmn"
End If


BotDetect.HelpLinkMode = BDC_HelpLinkModes("Image")

%>