User

User:Arjuninfo

From Joomla! Documentation

Tutorial To Create A Captcha Component For Joomla 1.5


First Step

Create A Folder Named com_captcha inside

root/components/

So it will looks like

root/components/com_captcha/


Inside com_captcha folder

create two directories named


models

views




then inside the

\components\com_captcha


create two php files and 1 html file


captcha.php

controller.php

and

index.html


captcha.php


<?php


// no direct access defined( '_JEXEC' ) or die( 'Restricted access' );

// DEVNOTE: Show helloworld text. // ianmac opened a discussion about MVC. Please go here for more information. // index.php?topic=136576.0 // Please DO more version of my component.

require_once (JPATH_COMPONENT.DS.'controller.php');

// Require specific controller if requested if($controller = JRequest::getVar('controller')) {

require_once (JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php'); }


// Create the controller $classname = 'CaptchaController'.$controller; $controller = new $classname( );

// Perform the Request task $controller->execute( JRequest::getVar('task'));

// Redirect if set by the controller $controller->redirect(); ?>


controller.php


<?php


jimport('joomla.application.component.controller');

class CaptchaController extends JController{ /**

  • Method to display the view
  • @access public
  • /

function display() {

parent::display(); } }

?>


controller.php finished


Now enter into

components\com_captcha\models


here u need to create one php file and one html file


captcha.php

index.html


components\com_captcha\models\captcha.php


captcha.php


<?php


// Check to ensure this file is included in Joomla! defined('_JEXEC') or die();


jimport( 'joomla.application.component.model' );

class CatpchaModelCaptcha extends JModel {

/**

  • data array
  • @var array
  • /

var $_data;

/**

  • @access public
  • @return void
  • /

function __construct() { parent::__construct(); }


/**

  • Retrieves the data
  • @return array Array of objects containing the data from the database
  • /

}


components\com_captcha\models\captcha.php finished


components\com_captcha\models\index.htm


index.htm


<html><body bgcolor="#FFFFFF"></body></html>

index.html finished


then enter into this directory


components\com_captcha\views

create a directory named captcha


so the folder structure looks like

components\com_captcha\views\captcha


Inside

components\com_captcha\views\captcha\


you need to create one php file and html file

view.html.php

and

index.html


components\com_captcha\views\captcha\view.html.php

view.html.php


<?php


// Check to ensure this file is included in Joomla! defined('_JEXEC') or die();

jimport( 'joomla.application.component.view' );


class CaptchaViewCaptcha extends JView { /**

  • @var array
  • /

var $_data; var $font = 'images/CONMAN.ttf'; private $code;

/**

  • display method of Hello view
  • @return void
    • /

function display($tpl = null) { // session $session =& JFactory::getSession();

// Generate code $code = $this->generateCode(isset($_GET['characters'])?$_GET['characters']:5); // Generate image $this->getCaptchaSecurityImages($code, $_GET['width'],$_GET['height']);

// Set session $session->set('security_code', $code);

parent::display($tpl); exit; } function generateCode($characters) { /* list all possible characters, similar looking characters and vowels have been removed */ $possible = '23456789bcdfhQkmnrstvwxzJi'; $code = ; $i = 0; while ($i < $characters) { $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1); $i++; } return $code; }

function getCaptchaSecurityImages($code, $width='120',$height='40') { //$code = $this->generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.75; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 0, 120, 100);

//$background_color = imagecolorallocate($image, 123, 123, 123);

$text_color = imagecolorallocate($image, 20, 40, 100); $noise_color = imagecolorallocate($image, 100, 120, 180); /* generate random dots in background */ for( $i=0; $i<($width*$height)/3; $i++ ) { imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color); } /* generate random lines in background */ for( $i=0; $i<($width*$height)/150; $i++ ) { imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color); } /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4])/2; $y = ($height - $textbox[5])/2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); } function getCode(){ return $this->code; }

}


thats all u created a captcha


to add this is in every page

add this javascript

<script> if(document.getElementById("verify_code").value == 0){ alert("<?php echo JText::_( 'Please enter the Verification Code', true );?>"); document.getElementById("verify_code").focus(); return false; } </script>

and this code


<img src="index.php?option=com_captcha&width=150&height=50&characters=5" alt="" />
* Verification Code

<input class="inputbox req val number textbox_bg" name="verify_code" id="verify_code" type="text" style="height:16px; width:205px; background: #FFFFFF !important;" onchange="check_exist(this.value);" />


that all...........



Download Com_captcha component

http://rapidshare.com/files/290616453/files.rar


To retrive this captcha for a particular form

just add this code



<img src="index.php?option=com_captcha&width=150&height=50&characters=5" alt="" />
* Verification Code

<input class="inputbox req val number textbox_bg" name="verify_code" id="verify_code" type="text" style="height:16px; width:205px; background: #FFFFFF !important;" onchange="check_exist(this.value);" />


Dont forget to put the font in images folder....


Font Download

http://forum.joomla.org/download/file.php?id=63672