Welcome Guest, Not a member yet? Register   Sign In
an enhanced captcha as helper
#1

[eluser]Christian Rößler[/eluser]
Hy folks,

having played around with CI (for the first time) in a new project i found out that the shipped captcha-plugin did not fit my needs.
so i wrote a new captcha (after i wasn't able to find one on this site) - as helper - with support for:

- random background-colors
- random char-colors
- multiple font-files (ttf as before)
- set the font-size via config

The return-value is exactly the same as CI's captcha-plugin's.
create_captcha($config) returns an array with the html-img-tag, a timestamp and the word itself.
put the captcha_helper.php into
Code:
/system/application/helpers/
a sample controller-snippet:
Code:
$this->load->helper('captcha');
        
        // create the captcha-config
        $aCaptchaCfg = array(
            //'word'          => 'myrandomword',    //default: random()
            'length'        => 6,                         //default: 5
            'img_path'   => './captcha/',   //no default !
            'img_url'       => site_url().'captcha/',  // no default!
            'font_path'  => './system/fonts/', // default: ./system/fonts/
            'fonts'         => array('texb.ttf', 'TSCu_Comic.ttf'), // default: texb.ttf
            'font_size'     => 25,      // default: 18
            'img_width'  => '180',  // default: 170
            'img_height' => '60',   // default: 60
            'expiration' => 7200 // default: 7200
            );
        
        // get captcha-stuff
    $aCaptcha = create_captcha($aCaptchaCfg);

you will find the helper here: captcha_helper.php.tar.gz
feel free to make changes / enhancements and share your work!

bye,
chris

PS: CI rocks!
#2

[eluser]mironcho[/eluser]
Thanks Christian!
#3

[eluser]Christian Rößler[/eluser]
hy,

had a small typo, feel free to get the newest version from the same location as above.
#4

[eluser]JP X3M[/eluser]
very n00bish question... how do i add this to my site? thanks... i have not yet tried the built in captcha plugin, but upon reading this seems to be an improvement of the plugin so i wanna implement it also...

thanks so much!
#5

[eluser]mironcho[/eluser]
Hi JP X3M
Have you read Christian Rößler post carefully? You must create system/application/helpers/ folder and put captcha_helper in.
Then load it and use it as is described in first post Smile
#6

[eluser]JP X3M[/eluser]
i did excatly that and then in my register controller i have these settings...

Code:
function  index(){
        
        // create the captcha-config
        $aCaptchaCfg = array(
            //'word'          => 'myrandomword',    //default: random()
            'length'        => 6,                         //default: 5
            'img_path'   => site_url().'captcha/',   //no default !
            'img_url'       => site_url().'captcha/',  // no default!
            'font_path'  => site_url().'system/fonts/', // default: ./system/fonts/
            'fonts'         => array('corsiva.ttf', 'TSCu_Comic.ttf'), // default: texb.ttf
            'font_size'     => 15,      // default: 18
            'img_width'  => '180',  // default: 170
            'img_height' => '60',   // default: 60
            'expiration' => 7200 // default: 7200
            );
        
        // get captcha-stuff
    $aCaptcha = create_captcha($aCaptchaCfg);
    }

then i tried print_r($aCaptchaCfg) and it outputs the array so i guess it should be okay...

next should i echo it or anything? thanks for your help!

UPDATE:

oh im so sorry i forgot to read the original catpcha plugin... i read it just now and i will try it first... thanks for your help though... ^__^
#7

[eluser]Christian Rößler[/eluser]
Hy 'JP X3M',

First create a folder called
Code:
helpers
in
Code:
/system/application/
if not exists.
Then copy the helper-file into this directory.

After that create a writeable folder where the generated captcha-images will reside in:
Code:
/captcha/
Make shure this is writeable by the webserver / php-instance (chmod 777 for example).

Your example folder structure:
Code:
/system/
/system/application/
/system/application/helpers/
/system/application/helpers/captcha_helper.php
/captcha/

If you use apache's mod rewrite to hide index.php from your url, edit .htaccess to allow clients access to the captcha directory!

Finally write your controller (an example):
Code:
function  index(){
// load the captcha helper
$this->load->helper('captcha');

// create the captcha-config
$aCaptchaCfg = array(
  //'word'          => 'myrandomword',    //default: random()
  'length'        => 6,                         //default: 5
  'img_path'   => site_url().'captcha/',   //no default !
  'img_url'       => site_url().'captcha/',  // no default!
  'font_path'  => site_url().'system/fonts/', // default: ./system/fonts/
  'fonts'         => array('corsiva.ttf', 'TSCu_Comic.ttf'), // default: texb.ttf
  'font_size'     => 15,      // default: 18
  'img_width'  => '180',  // default: 170
  'img_height' => '60',   // default: 60
  'expiration' => 7200 // default: 7200
);
        
// get captcha-stuff
$aCaptcha = create_captcha($aCaptchaCfg);

// throw the captcha-array to your view and load the view
$data['aCaptcha'] = $aCaptcha;
$this->load->view('myview', $data);
}
Make sure to edit the line where you define the folders (img_path, font_path). Upload your own ttf-fonts into
Code:
/system/fonts/
.

Here is an example-view (really simple).
You'll have to improve this by using a form and validate the input (the captcha-word typed in by the user) and compare it with the generated captcha-secret.
Code:
<html>
<head><title>foobar</title></head>
<body>
  captcha-test:<br/>
  &lt;?=$aCaptcha['image']?&gt;<br/>
&lt;/body&gt;
&lt;/html&gt;

There are 3 keys in the generated Captcha-array:

Code:
$aCaptcha['image']
represents the image-html-tag to include the image-file into forms (<img src="..." />)

Code:
$aCaptcha['word']
The generated secret word. You'll need this one to compare the user-input with. If this word and the user-input are not equal, then republish the form because the captcha is wrong.

Code:
$aCaptcha['time']
The timecode when the captcha has been generated. I don't really need this...

Get more fonts here: http://www.fontasy.de/ (for example)
or here: google -> 'free fonts'
#8

[eluser]JP X3M[/eluser]
thanks... i finally got it to output the captcha image... thanks!
#9

[eluser]JOKERz[/eluser]
miss type line 65
Code:
if( !isset($aCfg['img_heigtht']) OR $aCfg['img_height'] == '')

height not heigtht Wink
#10

[eluser]Unknown[/eluser]
I followed the example above with the following code. It doesnt seem to work for me. I dont even get a print_r() !

My directory structure is as follows:

img_path = http://localhost/Paradise Calling/common/captcha/
img_url = http://localhost/Paradise Calling/common/captcha/
font_path = http://localhost/Paradise Calling/application/fonts/

Am I supposed to add place an image in one of those dirs or something?

The captcha helper is located at: http://localhost/Paradise Calling/application/helpers/

Code:
// Index page
    function index() {
        
        // Captcha section
        // Create the captcha-config
        $aCaptchaCfg = array(
            //'word'        => 'myrandomword',                            //default: random()
            'length'        => 6,                                         //default: 5
            'img_path'       => base_url().'common/captcha/',               //no default !
            'img_url'       => base_url().'common/captcha/',            // no default!
            'font_path'      => base_url().'application/fonts/',         // default: ./system/fonts/
            //'fonts'       => array('corsiva.ttf', 'TSCu_Comic.ttf'),     // default: texb.ttf
            'font_size'     => 15,                                      // default: 18
            'img_width'      => '180',                                      // default: 170
            'img_height'     => '60',                                       // default: 60
            'expiration'     => 7200                                     // default: 7200
        );
        
        // Get captcha-stuff
        $aCaptcha = create_captcha($aCaptchaCfg);
        
        print_r($aCaptcha);
        
        // Throw the captcha-array to your view and load the view
        $data['aCaptcha'] = $aCaptcha;

$this->load->view('listBlogs', $data);

My View code:

Code:
captcha-test:<br/>
    &lt;?php echo $aCaptcha['image']; ?&gt;<br/>

Ive made sure my paths are correct for img_path, img_url, font_path
Whats the problem with this?




Theme © iAndrew 2016 - Forum software by © MyBB