Fun Infused Games  |   Smooth Operator

  Home   |    Archive   |    About
Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

A Simple Animal CAPTCHA
Date 10/23/2008    Tags ASP.net    (2)

If you run a website that allows for comments or any other user submitted content, before long the ugly face of spam will present itself (obviously it hasn't happened yet on this site, but it will one day). Spammers will come to your site just once to setup their bots, but once created their bots will flood your pages daily with spam.

A good way to get around this is by using a CAPTCHA type input field. Essentially this takes a series of letters and presents them as a hard to read image. To submit any content, the CAPTCHA information needs to be entered in a field. Because of the scrambled nature of the picture, bots are unable to read it (or at least have a difficult time doing so).

As spam bots get better and better, the text in the CAPTCHA images gets harder and harder to read, sometimes to the point where it's unreadable to the human eye. This is something I hate and one possible remedy is by using easy to distinguish images for your CAPTCHAs instead. For instance, show a picture of a pig. Any (most) human should be able to look at the picture and type "pig" in a field, but it will prove much more difficult for a bot to determine that it's a pig.

I wanted something like this for a site I'm working on. Since traffic (and hopefully spam) will initially be low, I just put together a simple setup for this sort of thing.

CAPTCHA!


When you load the page, a random number is created. This number is saved to a hidden field and then used to determine which image to display for the CAPTCHA. Below is the code to do this.
protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
    {
        int CaptchaID = SmoothOperator.RandomNumber(1,4);
        hdnCaptchaID.Value = CaptchaID.ToString();

        switch (CaptchaID)
        {
            case 1:
                lblCaptchaPicture.Text = "<img src=images/captcha/lion.jpg />";
                break;

            case 2:
                lblCaptchaPicture.Text = "<img src=images/captcha/tiger.jpg />";
                break;

            case 3:
                lblCaptchaPicture.Text = "<img src=images/captcha/bear.jpg />";
                break;

            case 4:
                lblCaptchaPicture.Text = "<img src=images/captcha/owl.jpg />";
                break;
        }
    }
} 
On the ASPX page, you'll need a label for displaying the picture (some people would rather use an tag but I've been unable to kick the habit of using labels even when there is a much better control available) and a textbox for the user to enter the CAPTCHA data into.


What animal is this a picture of?<br />
<asp:Label ID="lblCaptchaPicture" runat="server" />
<asp:HiddenField ID="hdnCaptchaID" runat="server" 
                    

This article has been view 1437 times.


Comments

mm

Avatar

8/1/2014 3:10:30 AM

fff
D

Avatar

8/1/2014 3:10:57 AM

FF


Add Comments

Name *
Website
  Name the animal in the picture below:

*  
Comment *
Insert Cancel
Things To Click


Tags
Video Games (7)  Trivia or Die (3)  SQL (1)  iOS (3)  Game Dev (11)  Advise (14)  PC (1)  World of Chalk (2)  FIN (20)  Abduction Action! (27)  XBLIG (32)  Abduction Action (1)  Nastier (4)  ASP.net (18)  Absurd (2)  Volchaos (11)  Web (19)  Fin (1)  XNA (40)  Rant (50)  Cool (2)  Visual Studio (1)  Trivia Or Die (1)  Xbox (1)  C# (14)  Sports (11)  Design (2)  Development (13)  Hypership (28)  WP7 (8)  VolChaos (1)  Nasty (34)  Abdction Action! (1)