blob: be1b20aaf13ea1b94285e8793d8eafcff5847982 [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,
Preethambfa16442014-11-12 10:26:24 -050036 'font_size' => 16,
Preetham6157e522014-12-05 12:19:09 -050037 'img_id' => 'Imageid',
Andrey Andreev8963f402013-07-18 16:02:47 +030038 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
39
Andrey Andreevc0a1ce92013-07-18 16:05:02 +030040 // White background and border, black text and red grid
Andrey Andreev8963f402013-07-18 16:02:47 +030041 'colors' => array(
42 'background' => array(255, 255, 255),
43 'border' => array(255, 255, 255),
44 'text' => array(0, 0, 0),
45 'grid' => array(255, 40, 40)
46 )
Derek Jones8ede1a22011-10-05 13:34:52 -050047 );
48
Andrey Andreev48a86752012-11-08 15:16:34 +020049 $cap = create_captcha($vals);
50 echo $cap['image'];
Derek Jones8ede1a22011-10-05 13:34:52 -050051
52- The captcha function requires the GD image library.
Andrey Andreev48a86752012-11-08 15:16:34 +020053- Only the **img_path** and **img_url** are required.
54- If a **word** is not supplied, the function will generate a random
Derek Jones8ede1a22011-10-05 13:34:52 -050055 ASCII string. You might put together your own word library that you
56 can draw randomly from.
57- If you do not specify a path to a TRUE TYPE font, the native ugly GD
58 font will be used.
Andrey Andreev45965742014-08-27 20:40:11 +030059- The "captcha" directory must be writable
Andrey Andreev48a86752012-11-08 15:16:34 +020060- The **expiration** (in seconds) signifies how long an image will remain
Derek Jones8ede1a22011-10-05 13:34:52 -050061 in the captcha folder before it will be deleted. The default is two
62 hours.
ash29ae72d2013-04-10 13:59:42 +010063- **word_length** defaults to 8, **pool** defaults to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
Preethame2913652014-12-04 21:01:52 -050064- **font_size** defaults to 16, the native GD font has a size limit. Specify a "true type" font for bigger sizes.
65- The **img_id** will be set as the "id" of the captcha image.
Andrey Andreev8963f402013-07-18 16:02:47 +030066- If any of the **colors** values is missing, it will be replaced by the default.
Derek Jones8ede1a22011-10-05 13:34:52 -050067
68Adding a Database
69-----------------
70
71In order for the captcha function to prevent someone from submitting,
Andrey Andreev48a86752012-11-08 15:16:34 +020072you will need to add the information returned from ``create_captcha()``
73to your database. Then, when the data from the form is submitted by
74the user you will need to verify that the data exists in the database
75and has not expired.
Derek Jones8ede1a22011-10-05 13:34:52 -050076
Andrey Andreev48a86752012-11-08 15:16:34 +020077Here is a table prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -050078
79 CREATE TABLE captcha (  
80 captcha_id bigint(13) unsigned NOT NULL auto_increment,  
81 captcha_time int(10) unsigned NOT NULL,  
Bo-Yi Wuf3fddf62012-08-31 10:10:16 +080082 ip_address varchar(45) NOT NULL,  
Derek Jones8ede1a22011-10-05 13:34:52 -050083 word varchar(20) NOT NULL,  
84 PRIMARY KEY `captcha_id` (`captcha_id`),  
85 KEY `word` (`word`)
86 );
87
88Here is an example of usage with a database. On the page where the
Andrey Andreev48a86752012-11-08 15:16:34 +020089CAPTCHA will be shown you'll have something like this::
Derek Jones8ede1a22011-10-05 13:34:52 -050090
91 $this->load->helper('captcha');
92 $vals = array(     
93 'img_path' => './captcha/',     
94 'img_url' => 'http://example.com/captcha/'     
95 );
96
97 $cap = create_captcha($vals);
98 $data = array(     
99 'captcha_time' => $cap['time'],     
100 'ip_address' => $this->input->ip_address(),     
101 'word' => $cap['word']     
102 );
103
104 $query = $this->db->insert_string('captcha', $data);
105 $this->db->query($query);
106
107 echo 'Submit the word you see below:';
Derek Jonesb8c283a2013-07-19 16:02:53 -0700108 echo $cap['image'];
Derek Jones8ede1a22011-10-05 13:34:52 -0500109 echo '<input type="text" name="captcha" value="" />';
110
111Then, on the page that accepts the submission you'll have something like
Andrey Andreev48a86752012-11-08 15:16:34 +0200112this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
114 // First, delete old captchas
115 $expiration = time() - 7200; // Two hour limit
116 $this->db->where('captcha_time < ', $expiration)
Andrey Andreev48a86752012-11-08 15:16:34 +0200117 ->delete('captcha');
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
119 // Then see if a captcha exists:
Andrey Andreev48a86752012-11-08 15:16:34 +0200120 $sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?';
Derek Jones8ede1a22011-10-05 13:34:52 -0500121 $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
122 $query = $this->db->query($sql, $binds);
123 $row = $query->row();
124
125 if ($row->count == 0)
126 {     
Andrey Andreev48a86752012-11-08 15:16:34 +0200127 echo 'You must submit the word that appears in the image.';
Derek Jones333ad2e2013-07-19 17:03:25 -0700128 }
129
130Available Functions
131===================
132
133The following functions are available:
134
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200135.. php:function:: create_captcha([$data = ''[, $img_path = ''[, $img_url = ''[, $font_path = '']]]])
Derek Jones333ad2e2013-07-19 17:03:25 -0700136
137 :param array $data: Array of data for the CAPTCHA
Andrey Andreev44aa1ab2017-11-07 15:42:53 +0200138 :param string $img_path: Path to create the image in (DEPRECATED)
139 :param string $img_url: URL to the CAPTCHA image folder (DEPRECATED)
140 :param string $font_path: Server path to font (DEPRECATED)
Derek Jones333ad2e2013-07-19 17:03:25 -0700141 :returns: array('word' => $word, 'time' => $now, 'image' => $img)
Andrey Andreev3de130c2014-02-07 23:31:49 +0200142 :rtype: array
Derek Jones333ad2e2013-07-19 17:03:25 -0700143
144 Takes an array of information to generate the CAPTCHA as input and
145 creates the image to your specifications, returning an array of
146 associative data about the image.
147
148 ::
149
150 array(
151 'image' => IMAGE TAG
152 'time' => TIMESTAMP (in microtime)
153 'word' => CAPTCHA WORD
154 )
155
156 The **image** is the actual image tag::
157
158 <img src="http://example.com/captcha/12345.jpg" width="140" height="50" />
159
160 The **time** is the micro timestamp used as the image name without the
161 file extension. It will be a number like this: 1139612155.3422
162
163 The **word** is the word that appears in the captcha image, which if not
Andrey Andreev44aa1ab2017-11-07 15:42:53 +0200164 supplied to the function, will be a random string.
165
166 .. note:: Usage of the ``$img_path``, ``$img_url`` and ``$font_path``
167 parameters is DEPRECATED. Provide them in the ``$data`` array
168 instead.