Ever wondered how to add a captcha to an HTML contact page on your own website? With a simple script, you can easily join the ranks of websites that have captchas on their contact pages. Keep reading to see how it can be done.
Before I jump into the code to add a captcha to an HTML contact page, I would to answer the question on what a captcha actually is. Well, a captcha is an image with human readable code embedded that requires any human intending to submit a form to verify a predefined code by replicating it to assure the system that it is not an automated piece of software that is actually filling out a contact form. Captchas are widely used to combat spam at the client side level.
Components of a Captcha
Captchas consist of three main sections. The first is the part that resides on the client end or the rather the web browser and that is the part that the user interacts with. The second section is the one that generates the code and presents it in the form of an image file which is human-readable but poses a problem to software or machines trying to read it. The third section validates the code to ensure it matches the code it generated.
The HTML Source Code
To add a Captcha to the HTML contact page, you would use a simple piece of code like the one below and include it in your HTML contact page.
<IMG SRC="file/to/generate/captcha" ><P><INPUT TYPE="text" NAME="captcha" VALUE=""><INPUT TYPE="hidden" NAME="id" value="2Hehu4i2dh4KU6">
The code above has three elements to it. There is the image which is actually a link to the dynamic image generation script with parameters read from the ID shown in the third element above and mapped with a one time piece of text which is entered in the text field named “Captcha" in the HTML code above. That is all you need to do as far as the HTML portion of the code goes.
The Rest of the Captcha
The rest of the captcha will be run off the server, which has several components to it. The first component is the script which generates a random ID and random text that is mapped to the ID and stores it in a database.
The second component is the script that generates an image by reading the respective ID and generated random text and then creating an image based on that with some level of noise to it so as to make it difficult to read for anything other than human beings.
The third component is the component that compares the ID submitted with the contact form and the value typed in by the human being with the ID and corresponding text in the database. If they match, your form validates otherwise your form fails to validate and the user to retype the details.
The server side components of captchas involve some lengthy code but you need not worry as in almost all cases if you import captcha scripts you will not be required to edit any code on the server side but rather place the files in the correct location.
In many cases adding a captcha to a HTML contact page would require some additional dependencies to be installed onto your server which mostly consist of the your web server's ability to dynamically generate images on the fly. To get information on whether you have a compatible server you may have to contact you hosting service or your systems administrator.