blob: 556434a500c3c857f1f2a5b0cf5ee8088d07b41f [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
Derek Jones333ad2e2013-07-19 17:03:25 -07008.. contents::
9 :local:
10
11.. raw:: html
12
13 <div class="custom-index container"></div>
Derek Jones8ede1a22011-10-05 13:34:52 -050014
15Loading this Helper
16===================
17
Derek Jones333ad2e2013-07-19 17:03:25 -070018This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050019
20 $this->load->helper('captcha');
21
Derek Jones8ede1a22011-10-05 13:34:52 -050022Using the CAPTCHA helper
Derek Jones333ad2e2013-07-19 17:03:25 -070023========================
Derek Jones8ede1a22011-10-05 13:34:52 -050024
Derek Jones333ad2e2013-07-19 17:03:25 -070025Once loaded you can generate a CAPTCHA like this::
Derek Jones8ede1a22011-10-05 13:34:52 -050026
Andrey Andreev48a86752012-11-08 15:16:34 +020027 $vals = array(
28 'word' => 'Random word',
29 'img_path' => './captcha/',
30 'img_url' => 'http://example.com/captcha/',
31 'font_path' => './path/to/fonts/texb.ttf',
32 'img_width' => '150',
33 'img_height' => 30,
ash3fd9bf82013-04-10 12:40:31 +010034 'expiration' => 7200,
ash29ae72d2013-04-10 13:59:42 +010035 'word_length' => 8,
Preetham83aeef12014-11-12 08:36:35 -050036 'font_size' =>16,
Andrey Andreev8963f402013-07-18 16:02:47 +030037 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
38
Andrey Andreevc0a1ce92013-07-18 16:05:02 +030039 // White background and border, black text and red grid
Andrey Andreev8963f402013-07-18 16:02:47 +030040 'colors' => array(
41 'background' => array(255, 255, 255),
42 'border' => array(255, 255, 255),
43 'text' => array(0, 0, 0),
44 'grid' => array(255, 40, 40)
45 )
Derek Jones8ede1a22011-10-05 13:34:52 -050046 );
47
Andrey Andreev48a86752012-11-08 15:16:34 +020048 $cap = create_captcha($vals);
49 echo $cap['image'];
Derek Jones8ede1a22011-10-05 13:34:52 -050050
51- The captcha function requires the GD image library.
Andrey Andreev48a86752012-11-08 15:16:34 +020052- Only the **img_path** and **img_url** are required.
53- If a **word** is not supplied, the function will generate a random
Derek Jones8ede1a22011-10-05 13:34:52 -050054 ASCII string. You might put together your own word library that you
55 can draw randomly from.
56- If you do not specify a path to a TRUE TYPE font, the native ugly GD
57 font will be used.
Andrey Andreev45965742014-08-27 20:40:11 +030058- The "captcha" directory must be writable
Andrey Andreev48a86752012-11-08 15:16:34 +020059- The **expiration** (in seconds) signifies how long an image will remain
Derek Jones8ede1a22011-10-05 13:34:52 -050060 in the captcha folder before it will be deleted. The default is two
61 hours.
ash29ae72d2013-04-10 13:59:42 +010062- **word_length** defaults to 8, **pool** defaults to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
Preetham83aeef12014-11-12 08:36:35 -050063- **font_size** defaults to 16, the native GD font has a size limit. Specify a TRUE TYPE font for bigger sizes.
Andrey Andreev8963f402013-07-18 16:02:47 +030064- If any of the **colors** values is missing, it will be replaced by the default.
Derek Jones8ede1a22011-10-05 13:34:52 -050065
Preetham83aeef12014-11-12 08:36:35 -050066
Derek Jones8ede1a22011-10-05 13:34:52 -050067Adding a Database
68-----------------
69
70In order for the captcha function to prevent someone from submitting,
Andrey Andreev48a86752012-11-08 15:16:34 +020071you will need to add the information returned from ``create_captcha()``
72to your database. Then, when the data from the form is submitted by
73the user you will need to verify that the data exists in the database
74and has not expired.
Derek Jones8ede1a22011-10-05 13:34:52 -050075
Andrey Andreev48a86752012-11-08 15:16:34 +020076Here is a table prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -050077
78 CREATE TABLE captcha (  
79 captcha_id bigint(13) unsigned NOT NULL auto_increment,  
80 captcha_time int(10) unsigned NOT NULL,  
Bo-Yi Wuf3fddf62012-08-31 10:10:16 +080081 ip_address varchar(45) NOT NULL,  
Derek Jones8ede1a22011-10-05 13:34:52 -050082 word varchar(20) NOT NULL,  
83 PRIMARY KEY `captcha_id` (`captcha_id`),  
84 KEY `word` (`word`)
85 );
86
87Here is an example of usage with a database. On the page where the
Andrey Andreev48a86752012-11-08 15:16:34 +020088CAPTCHA will be shown you'll have something like this::
Derek Jones8ede1a22011-10-05 13:34:52 -050089
90 $this->load->helper('captcha');
91 $vals = array(     
92 'img_path' => './captcha/',     
93 'img_url' => 'http://example.com/captcha/'     
94 );
95
96 $cap = create_captcha($vals);
97 $data = array(     
98 'captcha_time' => $cap['time'],     
99 'ip_address' => $this->input->ip_address(),     
100 'word' => $cap['word']     
101 );
102
103 $query = $this->db->insert_string('captcha', $data);
104 $this->db->query($query);
105
106 echo 'Submit the word you see below:';
Derek Jonesb8c283a2013-07-19 16:02:53 -0700107 echo $cap['image'];
Derek Jones8ede1a22011-10-05 13:34:52 -0500108 echo '<input type="text" name="captcha" value="" />';
109
110Then, on the page that accepts the submission you'll have something like
Andrey Andreev48a86752012-11-08 15:16:34 +0200111this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
113 // First, delete old captchas
114 $expiration = time() - 7200; // Two hour limit
115 $this->db->where('captcha_time < ', $expiration)
Andrey Andreev48a86752012-11-08 15:16:34 +0200116 ->delete('captcha');
Derek Jones8ede1a22011-10-05 13:34:52 -0500117
118 // Then see if a captcha exists:
Andrey Andreev48a86752012-11-08 15:16:34 +0200119 $sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?';
Derek Jones8ede1a22011-10-05 13:34:52 -0500120 $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
121 $query = $this->db->query($sql, $binds);
122 $row = $query->row();
123
124 if ($row->count == 0)
125 {     
Andrey Andreev48a86752012-11-08 15:16:34 +0200126 echo 'You must submit the word that appears in the image.';
Derek Jones333ad2e2013-07-19 17:03:25 -0700127 }
128
129Available Functions
130===================
131
132The following functions are available:
133
134.. function:: create_captcha([$data = ''[, $img_path = ''[, $img_url = ''[, $font_path = '']]]])
135
136 :param array $data: Array of data for the CAPTCHA
137 :param string $img_path: Path to create the image in
138 :param string $img_url: URL to the CAPTCHA image folder
139 :param string $font_path: Server path to font
140 :returns: array('word' => $word, 'time' => $now, 'image' => $img)
Andrey Andreev3de130c2014-02-07 23:31:49 +0200141 :rtype: array
Derek Jones333ad2e2013-07-19 17:03:25 -0700142
143 Takes an array of information to generate the CAPTCHA as input and
144 creates the image to your specifications, returning an array of
145 associative data about the image.
146
147 ::
148
149 array(
150 'image' => IMAGE TAG
151 'time' => TIMESTAMP (in microtime)
152 'word' => CAPTCHA WORD
153 )
154
155 The **image** is the actual image tag::
156
157 <img src="http://example.com/captcha/12345.jpg" width="140" height="50" />
158
159 The **time** is the micro timestamp used as the image name without the
160 file extension. It will be a number like this: 1139612155.3422
161
162 The **word** is the word that appears in the captcha image, which if not
163 supplied to the function, will be a random string.