Showing posts with label ASP. Show all posts
Showing posts with label ASP. Show all posts

Thursday, April 15, 2010

Implement reCAPTCHA with Classic ASP and Validate with AJAX

A friend asked me to help implementing reCAPTCHA with classic ASP which I successfully managed to implement even though I have not dabbled much with ASP - read the implementation here.
So now the expectations rise and he asks me to validate reCaptcha using AJAX - I do not have any experience of AJAX combined with ASP either.

Going through the resources section of the recaptcha.net site did not help much in this regard. So here are the steps that I did to achieve this. Note that most parts of the solution here have been taken from various pages on the net which are listed at the bottom of this post in the References section.


There are 6 parts of the code as follows:
  1. JavaScript to create reCaptcha on the page that has the input form
  2. Insert the created reCaptcha in the desired location
  3. Generic JavaScript to make AJAX call
  4. Make AJAX call to Server-side component to validate reCaptcha
  5. Handle AJAX validation response from server-side component
  6. Server side component to validate the reCaptcha

1. JavaScript to create reCaptcha on the page that has the input form
Here is the code that needs to be placed in the ASP file to generate the JavaScript which when called, generates the code for reCaptcha. The generated string needs to be placed in your form where you want the reCaptcha to be displayed.

<%
recaptcha_public_key = "XXXXXXXXXXXXXXXX"
function recaptcha_challenge_writer(publickey) 
  recaptcha_challenge_writer = "<script type=""text/javascript"">" & _ 
 "var RecaptchaOptions = {" & _ 
 " theme : 'red'," & _ 
 " tabindex : 0" & _ 
 "};" & _ 
 "</script>" & _ 
 "<script type=""text/javascript"" src=""http://api.recaptcha.net/challenge?k=" & publickey & """></script>" & _ 
 "<noscript>" & _ 
 "<iframe src=""http://api.recaptcha.net/noscript?k=" & publickey & """ frameborder=""1""></iframe>
 " & _ 
 "<textarea name=""recaptcha_challenge_field"" id=""recaptcha_challenge_field"" rows=""3"" cols=""40""></textarea>" & _ 
 "<input type=""hidden"" name=""recaptcha_response_field"" id=""recaptcha_response_field"" value=""manual_challenge"">" & _ 
 "</noscript>" 
end function 
%>

2. Insert the created reCaptcha in the desired location
Here is how you need to place the code in your form where you want the reCaptcha to be displayed.

<%=recaptcha_challenge_writer(recaptcha_public_key)%>

3. Generic JavaScript to make AJAX call
Here is a generic JavaScript function to make an AJAX call


4. Make AJAX call to Server-side component to validate reCaptcha
Here is the JavaScript "onClick" function that will make the specific call to the server-side component to validate the reCaptcha input.



5. Handle AJAX validation response from server-side component
Here code that needs to be put in the asp file that processes the submitted form.

// The function for handling the response from the server
var showMessageResponse = function (oXML) { 
    
    // get the response text, into a variable
 var response = oXML.responseText;
    
    // act on the result from the server
 if (response == "PASS") {
  //alert("Captcha Passed ...");
  document.getElementById("Form1").action = "process_form.asp";
  document.getElementById("Form1").submit();
 }  else {
        // Reload the captcha image if the validation failed & alert the user
 alert("Captcha Failed. Please try again ...");
 Recaptcha.reload();
 }
};

6. Server side component to validate the reCaptcha
Here code that needs to be put in the asp file that processes the submitted form.

'Define some variables to be used on page
dim pubkey, privkey, challenge, form_response, test_captcha, recaptcha_confirm

'Customize your public and private keys and other variables
pubkey = "XXXXXXXXXXXXXXXX"
privkey = "XXXXXXXXXXXXXXXX"

' Get the user input
challenge = Request.Form("recaptcha_challenge_field")
form_response = Request.Form("recaptcha_response_field")

' Generate the POST string for reCaptcha
Dim VarString
VarString = _
"privatekey=" & privkey & _
"&remoteip=" & Request.ServerVariables("REMOTE_ADDR") & _
"&challenge=" & challenge & _
"&response=" & form_response

' Make an AJAX call to validate input reCaptcha
Dim objXmlHttp
Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
if isNull(objXmlHttp) then
Set objXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
end if
objXmlHttp.open "POST", "http://api-verify.recaptcha.net/verify", False
objXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objXmlHttp.send VarString

' Receive response from reCaptcha server
Dim ResponseString
ResponseString = split(objXmlHttp.responseText, vblf)
Set objXmlHttp = Nothing

if ResponseString(0) = "true" then
'They answered captcha correctly
    response.write("PASS")
else
'They answered captcha incorrectly
    response.write("FAIL")
end if


Note:
  1. Of course, you need to sign-up for a free reCaptcha account at reCaptcha.net.
  2. The above steps 1, 3, 4 & 5 can be put in the same script block.
  3. The above step 2 needs to be put in the place in the form where reCaptcha is to be shown.
  4. Remember to substitute the public & private keys with the ones that you get when you sign up for a free reCaptcha account.
  5. This is a very basic, no-frills version. You can modularize, clean-up, optimize and add more error-handling. But this should be good enough to get you started!

References:
  1. This Google Groups post
  2. reCaptcha resources page
  3. This post on Jeff Niblack's Blog
  4. This post on World of Code Blog
  5. This blog post on Dark Side of Carton.

Implementing reCaptcha in Classic ASP

A friend of mine was very impressed with reCaptcha and wanted me to help him implement on his website that uses classic ASP! Going through the posts mentioned in the reference below gave me a bit of an idea but the solution was not very clear. Here's how I finally implemented it...

There are 3 parts of the code as follows:
  1. JavaScript to create reCaptcha on the page that has the input form
  2. Insert the created reCaptcha in the desired location
  3. Server side component to validate the reCaptcha

1. JavaScript to create reCaptcha on the page that has the input form
Here is the code that needs to be placed in the ASP file to generate the JavaScript which when called, generates the code for reCaptcha. The generated string needs to be placed in your form where you want the reCaptcha to be displayed.

<%
recaptcha_public_key = "XXXXXXXXXXXXXXXX"
function recaptcha_challenge_writer(publickey) 
  recaptcha_challenge_writer = "<script type=""text/javascript"">" & _ 
 "var RecaptchaOptions = {" & _ 
 " theme : 'red'," & _ 
 " tabindex : 0" & _ 
 "};" & _ 
 "</script>" & _ 
 "<script type=""text/javascript"" src=""http://api.recaptcha.net/challenge?k=" & publickey & """></script>" & _ 
 "<noscript>" & _ 
 "<iframe src=""http://api.recaptcha.net/noscript?k=" & publickey & """ frameborder=""1""></iframe>
 " & _ 
 "<textarea name=""recaptcha_challenge_field"" id=""recaptcha_challenge_field"" rows=""3"" cols=""40""></textarea>" & _ 
 "<input type=""hidden"" name=""recaptcha_response_field"" id=""recaptcha_response_field"" value=""manual_challenge"">" & _ 
 "</noscript>" 
end function 
%>

2. Insert the created reCaptcha in the desired location
Here is how you need to place the code in your form where you want the reCaptcha to be displayed.

<%=recaptcha_challenge_writer(recaptcha_public_key)%>

3. Server side component to validate the reCaptcha
Here code that needs to be put in the asp file that processes the submitted form.

'Define some variables to be used on page
dim pubkey, privkey, challenge, form_response, test_captcha, recaptcha_confirm

'Customize your public and private keys and other variables
pubkey = "XXXXXXXXXXXXXXXX"
privkey = "XXXXXXXXXXXXXXXX"

' Get the user input
challenge = Request.Form("recaptcha_challenge_field")
form_response = Request.Form("recaptcha_response_field")

' Generate the POST string for reCaptcha
Dim VarString
VarString = _
"privatekey=" & privkey & _
"&remoteip=" & Request.ServerVariables("REMOTE_ADDR") & _
"&challenge=" & challenge & _
"&response=" & form_response

' Make an AJAX call to validate input reCaptcha
Dim objXmlHttp
Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
if isNull(objXmlHttp) then
Set objXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
end if
objXmlHttp.open "POST", "http://api-verify.recaptcha.net/verify", False
objXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objXmlHttp.send VarString

' Receive response from reCaptcha server
Dim ResponseString
ResponseString = split(objXmlHttp.responseText, vblf)
Set objXmlHttp = Nothing

if ResponseString(0) = "true" then
'They answered captcha correctly
''' Handle Success scenario
else
'They answered captcha incorrectly
''' Handle Failure scenario
end if


Note:
  1. Of course, you need to sign-up for a free reCaptcha account at reCaptcha.net.
  2. The code in step 3 above needs to be put in the top of the file which processes your submitted form - the asp file that is mentioned in the action of this particular form. The above code needs to be executed before anything else in that file.
  3. It's regular processing steps should be put in the if block (line 37) that has the comment that the user answered the captcha correctly.
  4. Error message display logic should go into the else block (line 40) that has the comment that the user answered the captcha incorrectly.
  5. Remember to substitute the public & private keys with the ones that you get when you sign up for a free reCaptcha account.
  6. This is a very basic, no-frills version. You can modularize, clean-up, optimize and add more error-handling. But this should be good enough to get you started!

References:
  1. This Google Groups post
  2. reCaptcha resources page
  3. This post on Jeff Niblack's Blog
  4. This post on World of Code Blog

Monday, March 22, 2010

Install WordPress on IIS7

A friend of mine wanted me help him install WordPress on his website - no big deal as WP is famous for it's 5 minute installs and so I figured this should be a cake walk for me. Little did I know that my friend's web-server was IIS7 on Windows Server 2008 and this would translate to a 5 hour install :(

Google, pointed me to a very good site that helped me out - trainsignaltraining.com.

Here are the components that you need to download:

Here are the Installation steps:
  1. Install & configure PHP
  2. Install & configure MySQL
  3. Install & configure PHPMyAdmin
  4. Install & configure URL Rewrite Extension
  5. Install & configure WordPress
  6. Configure Search Engine Friendly URLs on WordPress

However, here are a few "gotchas" that need to be taken care of:
  • For PHPMyAdmin to work, you may need to change in config.inc.php
    $cfg[$i]['host'] = ‘localhost’; to
    $cfg[$i]['host'] = ‘127.0.01’; or
    $cfg['Servers'][$i]['host'] = ‘127.0.0.1′
  • For PHPMyAdmin to work, you may need to change in config.inc.php
    controluser = ‘pma’ to
    controluser = ‘root’
  • For PHPMyAdmin to work, you may need to change windows\system32\drivers\etc\hosts file:
    Remove the comment (#) in front of the line 127.0.0.1
    Comment out the line ::1 localhost
  • Read the comments in all the pages as they have solutions to a lot of common problems

Note: I am assuming that IIS7 is already installed. If not, then there's a good tutorial for that as well here!

LinkWithin

Related Posts Plugin for WordPress, Blogger...