blob: f47173453949cdb5d4675906ada54c8bd696fc04 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##############
2CAPTCHA Helper
3##############
4
5The CAPTCHA Helper file contains functions that assist in creating
6CAPTCHA images.
7
8.. contents:: Page Contents
9
10Loading this Helper
11===================
12
13This helper is loaded using the following code
Derek Jones8ede1a22011-10-05 13:34:52 -050014::
15
16 $this->load->helper('captcha');
17
18The following functions are available:
19
Andrey Andreev48a86752012-11-08 15:16:34 +020020create_captcha()
21================
22
23.. php:function:: function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
24
25 :param array $data: Array of data for the CAPTCHA
26 :param string $img_path: Path to create the image in
27 :param string $img_url: URL to the CAPTCHA image folder
28 :param string $font_path: Server path to font
Andrey Andreev72b4b3c2013-10-21 14:44:57 +030029 :returns: array
Derek Jones8ede1a22011-10-05 13:34:52 -050030
31Takes an array of information to generate the CAPTCHA as input and
32creates the image to your specifications, returning an array of
33associative data about the image.
34
Derek Jones8ede1a22011-10-05 13:34:52 -050035::
36
Andrey Andreev48a86752012-11-08 15:16:34 +020037 array(
Andrey Andreev72b4b3c2013-10-21 14:44:57 +030038 'word' => CAPTCHA WORD,
39 'time' => TIMESTAMP (in microtime),
40 'image' => IMAGE TAG,
41 'filename' => IMAGE FILE NAME
Andrey Andreev48a86752012-11-08 15:16:34 +020042 )
Derek Jones8ede1a22011-10-05 13:34:52 -050043
Andrey Andreev48a86752012-11-08 15:16:34 +020044The **image** is the actual image tag::
Derek Jones8ede1a22011-10-05 13:34:52 -050045
46 <img src="http://example.com/captcha/12345.jpg" width="140" height="50" />
47
Andrey Andreev48a86752012-11-08 15:16:34 +020048The **time** is the micro timestamp used as the image name without the
Derek Jones8ede1a22011-10-05 13:34:52 -050049file extension. It will be a number like this: 1139612155.3422
50
Andrey Andreev48a86752012-11-08 15:16:34 +020051The **word** is the word that appears in the captcha image, which if not
Derek Jones8ede1a22011-10-05 13:34:52 -050052supplied to the function, will be a random string.
53
54Using the CAPTCHA helper
55------------------------
56
Andrey Andreev48a86752012-11-08 15:16:34 +020057Once loaded you can generate a captcha like this::
Derek Jones8ede1a22011-10-05 13:34:52 -050058
Andrey Andreev48a86752012-11-08 15:16:34 +020059 $vals = array(
60 'word' => 'Random word',
61 'img_path' => './captcha/',
62 'img_url' => 'http://example.com/captcha/',
63 'font_path' => './path/to/fonts/texb.ttf',
64 'img_width' => '150',
65 'img_height' => 30,
ash3fd9bf82013-04-10 12:40:31 +010066 'expiration' => 7200,
ash29ae72d2013-04-10 13:59:42 +010067 'word_length' => 8,
Andrey Andreev8963f402013-07-18 16:02:47 +030068 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
69
Andrey Andreevc0a1ce92013-07-18 16:05:02 +030070 // White background and border, black text and red grid
Andrey Andreev8963f402013-07-18 16:02:47 +030071 'colors' => array(
72 'background' => array(255, 255, 255),
73 'border' => array(255, 255, 255),
74 'text' => array(0, 0, 0),
75 'grid' => array(255, 40, 40)
76 )
Derek Jones8ede1a22011-10-05 13:34:52 -050077 );
78
Andrey Andreev48a86752012-11-08 15:16:34 +020079 $cap = create_captcha($vals);
80 echo $cap['image'];
Derek Jones8ede1a22011-10-05 13:34:52 -050081
82- The captcha function requires the GD image library.
Andrey Andreev48a86752012-11-08 15:16:34 +020083- Only the **img_path** and **img_url** are required.
84- If a **word** is not supplied, the function will generate a random
Derek Jones8ede1a22011-10-05 13:34:52 -050085 ASCII string. You might put together your own word library that you
86 can draw randomly from.
87- If you do not specify a path to a TRUE TYPE font, the native ugly GD
88 font will be used.
89- The "captcha" folder must be writable (666, or 777)
Andrey Andreev48a86752012-11-08 15:16:34 +020090- The **expiration** (in seconds) signifies how long an image will remain
Derek Jones8ede1a22011-10-05 13:34:52 -050091 in the captcha folder before it will be deleted. The default is two
92 hours.
ash29ae72d2013-04-10 13:59:42 +010093- **word_length** defaults to 8, **pool** defaults to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
Andrey Andreev8963f402013-07-18 16:02:47 +030094- If any of the **colors** values is missing, it will be replaced by the default.
Derek Jones8ede1a22011-10-05 13:34:52 -050095
96Adding a Database
97-----------------
98
99In order for the captcha function to prevent someone from submitting,
Andrey Andreev48a86752012-11-08 15:16:34 +0200100you will need to add the information returned from ``create_captcha()``
101to your database. Then, when the data from the form is submitted by
102the user you will need to verify that the data exists in the database
103and has not expired.
Derek Jones8ede1a22011-10-05 13:34:52 -0500104
Andrey Andreev48a86752012-11-08 15:16:34 +0200105Here is a table prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -0500106
107 CREATE TABLE captcha (  
108 captcha_id bigint(13) unsigned NOT NULL auto_increment,  
109 captcha_time int(10) unsigned NOT NULL,  
Bo-Yi Wuf3fddf62012-08-31 10:10:16 +0800110 ip_address varchar(45) NOT NULL,  
Derek Jones8ede1a22011-10-05 13:34:52 -0500111 word varchar(20) NOT NULL,  
112 PRIMARY KEY `captcha_id` (`captcha_id`),  
113 KEY `word` (`word`)
114 );
115
116Here is an example of usage with a database. On the page where the
Andrey Andreev48a86752012-11-08 15:16:34 +0200117CAPTCHA will be shown you'll have something like this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
119 $this->load->helper('captcha');
120 $vals = array(     
121 'img_path' => './captcha/',     
122 'img_url' => 'http://example.com/captcha/'     
123 );
124
125 $cap = create_captcha($vals);
126 $data = array(     
127 'captcha_time' => $cap['time'],     
128 'ip_address' => $this->input->ip_address(),     
129 'word' => $cap['word']     
130 );
131
132 $query = $this->db->insert_string('captcha', $data);
133 $this->db->query($query);
134
135 echo 'Submit the word you see below:';
136 echo $cap['image'];
137 echo '<input type="text" name="captcha" value="" />';
138
139Then, on the page that accepts the submission you'll have something like
Andrey Andreev48a86752012-11-08 15:16:34 +0200140this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
142 // First, delete old captchas
143 $expiration = time() - 7200; // Two hour limit
144 $this->db->where('captcha_time < ', $expiration)
Andrey Andreev48a86752012-11-08 15:16:34 +0200145 ->delete('captcha');
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
147 // Then see if a captcha exists:
Andrey Andreev48a86752012-11-08 15:16:34 +0200148 $sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?';
Derek Jones8ede1a22011-10-05 13:34:52 -0500149 $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
150 $query = $this->db->query($sql, $binds);
151 $row = $query->row();
152
153 if ($row->count == 0)
154 {     
Andrey Andreev48a86752012-11-08 15:16:34 +0200155 echo 'You must submit the word that appears in the image.';
ash3a66fac2013-04-14 14:38:03 +0100156 }