blob: 3cf51330b83f034383135dd7fb55e6a84e464e98 [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,
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'
Preethambfa16442014-11-12 10:26:24 -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
66Adding a Database
67-----------------
68
69In order for the captcha function to prevent someone from submitting,
Andrey Andreev48a86752012-11-08 15:16:34 +020070you will need to add the information returned from ``create_captcha()``
71to your database. Then, when the data from the form is submitted by
72the user you will need to verify that the data exists in the database
73and has not expired.
Derek Jones8ede1a22011-10-05 13:34:52 -050074
Andrey Andreev48a86752012-11-08 15:16:34 +020075Here is a table prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -050076
77 CREATE TABLE captcha (  
78 captcha_id bigint(13) unsigned NOT NULL auto_increment,  
79 captcha_time int(10) unsigned NOT NULL,  
Bo-Yi Wuf3fddf62012-08-31 10:10:16 +080080 ip_address varchar(45) NOT NULL,  
Derek Jones8ede1a22011-10-05 13:34:52 -050081 word varchar(20) NOT NULL,  
82 PRIMARY KEY `captcha_id` (`captcha_id`),  
83 KEY `word` (`word`)
84 );
85
86Here is an example of usage with a database. On the page where the
Andrey Andreev48a86752012-11-08 15:16:34 +020087CAPTCHA will be shown you'll have something like this::
Derek Jones8ede1a22011-10-05 13:34:52 -050088
89 $this->load->helper('captcha');
90 $vals = array(     
91 'img_path' => './captcha/',     
92 'img_url' => 'http://example.com/captcha/'     
93 );
94
95 $cap = create_captcha($vals);
96 $data = array(     
97 'captcha_time' => $cap['time'],     
98 'ip_address' => $this->input->ip_address(),     
99 'word' => $cap['word']     
100 );
101
102 $query = $this->db->insert_string('captcha', $data);
103 $this->db->query($query);
104
105 echo 'Submit the word you see below:';
Derek Jonesb8c283a2013-07-19 16:02:53 -0700106 echo $cap['image'];
Derek Jones8ede1a22011-10-05 13:34:52 -0500107 echo '<input type="text" name="captcha" value="" />';
108
109Then, on the page that accepts the submission you'll have something like
Andrey Andreev48a86752012-11-08 15:16:34 +0200110this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
112 // First, delete old captchas
113 $expiration = time() - 7200; // Two hour limit
114 $this->db->where('captcha_time < ', $expiration)
Andrey Andreev48a86752012-11-08 15:16:34 +0200115 ->delete('captcha');
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
117 // Then see if a captcha exists:
Andrey Andreev48a86752012-11-08 15:16:34 +0200118 $sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?';
Derek Jones8ede1a22011-10-05 13:34:52 -0500119 $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
120 $query = $this->db->query($sql, $binds);
121 $row = $query->row();
122
123 if ($row->count == 0)
124 {     
Andrey Andreev48a86752012-11-08 15:16:34 +0200125 echo 'You must submit the word that appears in the image.';
Derek Jones333ad2e2013-07-19 17:03:25 -0700126 }
127
128Available Functions
129===================
130
131The following functions are available:
132
133.. function:: create_captcha([$data = ''[, $img_path = ''[, $img_url = ''[, $font_path = '']]]])
134
135 :param array $data: Array of data for the CAPTCHA
136 :param string $img_path: Path to create the image in
137 :param string $img_url: URL to the CAPTCHA image folder
138 :param string $font_path: Server path to font
139 :returns: array('word' => $word, 'time' => $now, 'image' => $img)
Andrey Andreev3de130c2014-02-07 23:31:49 +0200140 :rtype: array
Derek Jones333ad2e2013-07-19 17:03:25 -0700141
142 Takes an array of information to generate the CAPTCHA as input and
143 creates the image to your specifications, returning an array of
144 associative data about the image.
145
146 ::
147
148 array(
149 'image' => IMAGE TAG
150 'time' => TIMESTAMP (in microtime)
151 'word' => CAPTCHA WORD
152 )
153
154 The **image** is the actual image tag::
155
156 <img src="http://example.com/captcha/12345.jpg" width="140" height="50" />
157
158 The **time** is the micro timestamp used as the image name without the
159 file extension. It will be a number like this: 1139612155.3422
160
161 The **word** is the word that appears in the captcha image, which if not
162 supplied to the function, will be a random string.