PHP Captcha using jQuery AJAX

Generally We use Captcha in the form to ensure that the form is submitted with manual intervention without any tools or programs. In this article we will show PHP captcha using jQuery AJAX. Using jQuery we can also refresh the captcha code by resending AJAX call to generate new captcha image.

DOWNLOAD SOURCE CODE

PHP Captcha using jQuery AJAX

Refresh PHP Captcha using jQuery AJAX

Steps :
1. Create a PHP or HTML page with a form
2. Include JQuery file in Head tag of the file
3. Include CSS/Script code given below
4. Create captcha_code.php file with below given code for captcha creation

Step1: Form with Captcha
This code is for displaying a form with refreshable captcha image.

Step2: Include JQuery lib

It is included locally but we can include it by online also.

Step3: Include Scripts
Here we are validating Captcha code on click of send button.
On click of refresh button we are refreshing the captcha and creating a new image with new captcha code.

Step 4: Captcha Image Creation using PHP
In server side PHP script, we are using PHP rand() to generate random number. This random number will be encrypted by using md5() and cropped into 6 character captcha code. Then this code will be added to PHP session and as a source of captcha image by using PHP GD functions. This session will be used to validate captcha code where we are posting the form.

Before processing the form at server side, you can validate the user submitted captcha in the following manner:

This it the way to implement PHP Captcha using jQuery AJAX where you can render and refresh captcha image using AJAX for your form. 

DOWNLOAD SOURCE CODE

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.