<!-- '// Filename:
captchaForm.aspx '// Author: Adnan Masood '// Last updated: 12/08/03 --> <%@ Import
Namespace="System" %> <%@ Import
Namespace="System.Data" %> <%@ Import
Namespace="System.Data.OleDb" %> <%@ Page
Language="C#" debug=true%> <HTML> <head> <script runat="server"> // Pageload event void Page_Load(object Source, EventArgs E) { // If form is not posted
than assign the selectWord return to session if (!IsPostBack) Session["param"] =selectWord(); // Else check if session's
value is same as that of Captcha's entered value // in the form and assign
appropriate message to innertHtml of label else { if (String.Compare(Convert.ToString(Session["param"]),Convert.ToString(Request.Form["CAPTCHA"]))==0) lblstatus.InnerHtml= "Thanks for
Registeration with our Forums"; else lblstatus.InnerHtml= "Error Occured! Value of Image is not
correct, please <a href=captchaform.aspx> re-enter </a>"; } }
// Select word method // Return type String of random
word from dictionary // Dictionary is based on OGDEN's
BASIC ENGLISH //
http://ogden.basic-english.org/basiceng.html public String selectWord ()
{
// The Connection string
referencing the MDB file String ConnectionString =
"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +
Server.MapPath("dictionary.mdb") + ";";
// Datareader object OleDbDataReader objReader;
// Creating an array of 26
characters (alphabets in dictionary database as columns) char[] columns = newchar[26];
// Adding the column names
in the array // uses the ASCII character
conversion for selecting values // from A- Z for (int a=65; a<65+26; a++) columns[a-65]
= (char)a;
// Query String for
selecting a random column from spelling list database String QuerySQL = "SELECT " + columns[(new Random().Next(26))] + " FROM
spellList";
// Opening the connection OleDbConnection objConn = new OleDbConnection(ConnectionString); // Creating new command object OleDbCommand
objCmd = new OleDbCommand(); // Assigning command text objCmd.CommandText = QuerySQL; // Assigning the connection to command object connection
attribute objCmd.Connection = objConn;
// Instantiating a random class
object Random randomSeed = new Random(); // Creating a random seed
selector int randomSeedSelector=0; // An string character with
maximum capacity for dictionary column String[]
selectedIndex = new String[700]; String str = "";
// This code segment opens the
connection and read the dictionary try { objConn.Open(); objReader = objCmd.ExecuteReader(); while (objReader.Read()) { str = objReader.GetValue(0).ToString(); if (str.Length != 0) { selectedIndex[randomSeedSelector]
=str; randomSeedSelector++; } }// Ends While
str = selectedIndex[randomSeed.Next(randomSeedSelector)];
} // Ends Try
catch (Exception Err) { // The Error Catching
operations } finally { objConn.Close(); }
// Returns the selected
string return (str);
}// ends function </script> </head> <body> <% //Checking for
postback for initial processing of form. if (!IsPostBack) {
%> <form runat="server"> <P
align="left"><STRONG><FONT size="5">Signup for Forums</FONT></STRONG></P> <P> <TABLE id="Table1"
height="200" cellSpacing="1" cellPadding="1"
width="704" border="1"> <TR> <TD> <P>Name</P> </TD> <TD><INPUT
id="Text1" type="text"
name="name"></TD> </TR> <TR> <TD>Email Address</TD> <TD><INPUT
id="Text2" type="text"
name="email"></TD> </TR> <TR> <TD> <P><IMG
src="http://localhost/captcha/example2/captcha.aspx"></P> <P>Enter the
text above into the box on right.</P> </TD> <TD><INPUT
id="Text3" type="text"
name="CAPTCHA"></TD> </TR> <TR> <TD><INPUT
id="Submit1" type="submit" value="Submit"
name="Submit1"><INPUT id="Reset1"
type="reset" value="Reset"
name="Reset1"></TD> <TD></TD> </TR> </TABLE> </P> </form> <% } //Setting lable's
status accordingly else { %> <td id="lblstatus"
runat="server"></td> <% }%> </body> </HTML> |