In this article we will create a registration page with captcha implemented to it. Before going forward we should know what is captcha?
CAPTCHA: - Completely Automated Public Turing test to tell Computers and Humans Apart. A captcha is a program that protects websites against bots by generating and grading tests that humans can pass but current computer programs cannot. For example, humans can read distorted text but current computer programs can't.
Characteristics: A captcha is a means of automatically generating challenges which intends to:
- Provide a problem easy enough for all humans to solve.
- Prevent standard automated software from filling out a form, unless it is specially designed to circumvent specific CAPTCHA systems.
For more information please visit
http://www.captcha.net
http://en.wikipedia.org/wiki/CAPTCHA
Table structure
Create two tables employee and country as below:
Program
Default.aspx code
Default.aspx.vb code
- Imports System.Data
- Imports System.Data.SqlClient
- Partial Class _Default
- Inherits System.Web.UI.Page
- Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
- Dim sqlda As SqlDataAdapter
- Dim com As SqlCommand
- Dim ds As DataSet
- Dim dt As DataTable
- Dim str As String
- Dim con As New SqlConnection(strConnString)
-
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) HandlesMe.Load
- If Not Page.IsPostBack Then
- con.Open()
- str = "select * from country"
- com = New SqlCommand(str, con)
- Dim reader As SqlDataReader = com.ExecuteReader
- While reader.Read
- d1.Items.Add(reader("countryname"))
-
- End While
- reader.Close()
- con.Close()
- End If
- End Sub
- Protected Sub b1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles b1.Click
- Try
- con.Open()
- If Session("randomStr").ToString() = txtcaptcha.Text Then
-
- str = "insert into employee values ('" & rb1.SelectedValue & "','" & txtname.Text & "','" & txtemail.Text & "','" & txtdateofbirth.Text & "','" & rb2.SelectedValue & "','" & txtcompany.Text & "','"& txtdeg.Text & "','" & txtaddress.Text & "','" & txtcity.Text & "','" & txtstate.Text & "','" & txtzip.Text & "'," & txtphone.Text & "," & txtfax.Text & ",'" & txtwebsite.Text & "','" & d1.SelectedValue & "','" & txtcaptcha.Text & "')"
-
- com = New SqlCommand(str, con)
- com.ExecuteNonQuery()
- Response.Redirect("Success.aspx")
- con.Close()
- Else
- Response.Write("Wrong text inserted ,Please enter new characters shown in image textbox")
- End If
- Catch err As Exception
- Response.Write(err.Message)
- End Try
- End Sub
- End Class
Captcha.aspx code
- <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Captcha.aspx.vb" Inherits="Captcha"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- </div>
- </form>
- </body>
- </html>
Captcha.aspx.vb code
- Imports System.Drawing
- Imports System.Drawing.Imaging
-
- Imports System.Drawing.Text
- Partial Class Captcha
- Inherits System.Web.UI.Page
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) HandlesMe.Load
-
- Dim objBMP As Bitmap = New Bitmap(180, 51)
-
- Dim objGraphics As Graphics = Graphics.FromImage(objBMP)
- objGraphics.Clear(Color.OrangeRed)
- objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias
- Dim objFont As Font = New Font("arial", 30, FontStyle.Regular)
-
- Dim randomStr As String = GeneratePassword()
-
- Session.Add("randomStr", randomStr)
- objGraphics.DrawString(randomStr, objFont, Brushes.White, 2, 2)
- Response.ContentType = "image/GIF"
- objBMP.Save(Response.OutputStream, ImageFormat.Gif)
- objFont.Dispose()
- objGraphics.Dispose()
- objBMP.Dispose()
- End Sub
- Public Function GeneratePassword() As String
-
-
- Dim allowedChars As String = "a,b,c,d,e,f,g,h,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,"
- allowedChars += "A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z,"
- allowedChars += "2,3,4,5,6,7,8,9"
- Dim sep() As Char = {","c}
- Dim arr() As String = allowedChars.Split(sep)
- Dim passwordString As String = ""
- Dim temp As String
- Dim rand As Random = New Random()
- Dim i As Integer
- For i = 0 To 6 - 1 Step i + 1
- temp = arr(rand.Next(0, arr.Length))
- passwordString += temp
- Next
- Return passwordString
- End Function
- End Class
Output