Use Fox Captcha with forms

by saidbakr
Fox_captcha is a class that I written it. It is published at [url=http://www.phpclasses.org]PHP Classes[/url]. It offers creating captcha verification images for forms and it supports reloading feature. In this article, I will show you how to hack it to use it as a captcha for your applications forms.

Go to the following url and then download the package of Fox_captcha:
http://www.phpclasses.org/package/3949-PHP-Generate-CAPTCHA-validation-images-with-reload-.html

After extracting the package, you will count 7 files. We just need 3 files to make it works perfectly:

  • Fox_captcha.php - the main class file -
  • Fox_captcha_image.php - the reloaded image -
  • fox_captcha_image_loading.gif - the new feature of version 2.5 which is animated gif works as waiting -
In addition to the above three files, I encourage you to read how2use.html and test it using the index.php and form.php


Working with cake

  • Copy the file named Fox_captcha.php to the root of yourapp/vendors folder.
  • Copy the two files named Fox_captcha_image.php and fox_captcha_image_loading.gif to the root of yourapp/webroot
  • In the view dedicated to render your form, add a new text field to handle the user's verification code entry. In this step, you should know the name of the field. For example, consider the following:

<input type="text" name="data[Contact][vcode]" />
<?php
App
::import('Vendor','FoxCaptcha', array('file' => 'Fox_captcha.php'));
       
$cap = new Fox_captcha(100,30,5);
      
// $cap->lines_amount = 6;
       
$cap->en_reload 'Change';
       
$cap->make_it();
?>
In the above code we create the form and we initiate the object of class where the Captcha image will be produced next to the form with a JavaScript link on its right side for reloading the image.
  • Now we will do little changes - approx two lines of code - to the file named Fox_captcha_image.php in the webroot folder. From the package, Fox_captcha_image.php has the following code - till version 2.5 -:
///

<?php
session_start
();
include(
'Fox_captcha.php');
$img = new Fox_captcha($_SESSION['Fox_CaptchaSSim_width'], $_SESSION['Fox_CaptchaSSim_height'], $_SESSION['Fox_CaptchaSSim_codeLength']);
$img->lines_amount $_SESSION['Fox_CaptchaSSim_linesAmount'];
$img->make_it('JPG');
?> 
///
We just have to add one line of code before the first line and then modify the resulting third line - the include - to become as follows:

<?php
session_name
('CAKEPHP');
session_start();
include(
'../vendors/Fox_captcha.php');
$img = new Fox_captcha($_SESSION['Fox_CaptchaSSim_width'], $_SESSION['Fox_CaptchaSSim_height'], $_SESSION['Fox_CaptchaSSim_codeLength']);
$img->lines_amount $_SESSION['Fox_CaptchaSSim_linesAmount'];
$img->make_it('JPG');
?>


In the last step we set the session name to the CakePHP session name. It is by default CAKEPHP. However,
you should check yourapp/config/core.php file and search for "Configure::write('Session.cookie" and then

look at its second parameter value which is the name of CakePHP.

We included the main class file using the include('../vendors/Fox_captcha.php').


There is only one step should be done in the controller's action that receive the form input. You just need to initiate an object of the class and then run the test method to check if the user enter wrong or correct code:

<?php
App
::import('Vendor','FoxCaptcha',array('file'=>'fox_captcha.php'));
       
$test = new Fox_captcha(1,1,1); 
if (!empty(
$this->data)){
           if (
$test->test($this->data['Contact']['vcode'])){
//complete your logic
}
?>
We initiated an object called test and it will use the test method of the class which compare between the code entered by the user and the code generated by the object in the view page. If it is identical, it returns true if not it returns false

That's all. I hope it will be useful for you.

Report

More on Tutorials

Advertising

Comments

  • usman posted on 11/12/10 06:07:11 AM
    Hi,

    I tried this code but it's not showing the verification image - any help will be appreciated.

    Thanks,
login to post a comment.