blob: d83490b8ed335552cef81187162478ac6cac436f [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,
Andrey Andreev8963f402013-07-18 16:02:47 +030036 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
37
Andrey Andreevc0a1ce92013-07-18 16:05:02 +030038 // White background and border, black text and red grid
Andrey Andreev8963f402013-07-18 16:02:47 +030039 'colors' => array(
40 'background' => array(255, 255, 255),
41 'border' => array(255, 255, 255),
42 'text' => array(0, 0, 0),
43 'grid' => array(255, 40, 40)
44 )
Derek Jones8ede1a22011-10-05 13:34:52 -050045 );
46
Andrey Andreev48a86752012-11-08 15:16:34 +020047 $cap = create_captcha($vals);
48 echo $cap['image'];
Derek Jones8ede1a22011-10-05 13:34:52 -050049
50- The captcha function requires the GD image library.
Andrey Andreev48a86752012-11-08 15:16:34 +020051- Only the **img_path** and **img_url** are required.
52- If a **word** is not supplied, the function will generate a random
Derek Jones8ede1a22011-10-05 13:34:52 -050053 ASCII string. You might put together your own word library that you
54 can draw randomly from.
55- If you do not specify a path to a TRUE TYPE font, the native ugly GD
56 font will be used.
57- The "captcha" folder must be writable (666, or 777)
Andrey Andreev48a86752012-11-08 15:16:34 +020058- The **expiration** (in seconds) signifies how long an image will remain
Derek Jones8ede1a22011-10-05 13:34:52 -050059 in the captcha folder before it will be deleted. The default is two
60 hours.
ash29ae72d2013-04-10 13:59:42 +010061- **word_length** defaults to 8, **pool** defaults to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
Andrey Andreev8963f402013-07-18 16:02:47 +030062- If any of the **colors** values is missing, it will be replaced by the default.
Derek Jones8ede1a22011-10-05 13:34:52 -050063
64Adding a Database
65-----------------
66
67In order for the captcha function to prevent someone from submitting,
Andrey Andreev48a86752012-11-08 15:16:34 +020068you will need to add the information returned from ``create_captcha()``
69to your database. Then, when the data from the form is submitted by
70the user you will need to verify that the data exists in the database
71and has not expired.
Derek Jones8ede1a22011-10-05 13:34:52 -050072
Andrey Andreev48a86752012-11-08 15:16:34 +020073Here is a table prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -050074
75 CREATE TABLE captcha (  
76 captcha_id bigint(13) unsigned NOT NULL auto_increment,  
77 captcha_time int(10) unsigned NOT NULL,  
Bo-Yi Wuf3fddf62012-08-31 10:10:16 +080078 ip_address varchar(45) NOT NULL,  
Derek Jones8ede1a22011-10-05 13:34:52 -050079 word varchar(20) NOT NULL,  
80 PRIMARY KEY `captcha_id` (`captcha_id`),  
81 KEY `word` (`word`)
82 );
83
84Here is an example of usage with a database. On the page where the
Andrey Andreev48a86752012-11-08 15:16:34 +020085CAPTCHA will be shown you'll have something like this::
Derek Jones8ede1a22011-10-05 13:34:52 -050086
87 $this->load->helper('captcha');
88 $vals = array(     
89 'img_path' => './captcha/',     
90 'img_url' => 'http://example.com/captcha/'     
91 );
92
93 $cap = create_captcha($vals);
94 $data = array(     
95 'captcha_time' => $cap['time'],     
96 'ip_address' => $this->input->ip_address(),     
97 'word' => $cap['word']     
98 );
99
100 $query = $this->db->insert_string('captcha', $data);
101 $this->db->query($query);
102
103 echo 'Submit the word you see below:';
Derek Jonesb8c283a2013-07-19 16:02:53 -0700104 echo $cap['image'];
Derek Jones8ede1a22011-10-05 13:34:52 -0500105 echo '<input type="text" name="captcha" value="" />';
106
107Then, on the page that accepts the submission you'll have something like
Andrey Andreev48a86752012-11-08 15:16:34 +0200108this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500109
110 // First, delete old captchas
111 $expiration = time() - 7200; // Two hour limit
112 $this->db->where('captcha_time < ', $expiration)
Andrey Andreev48a86752012-11-08 15:16:34 +0200113 ->delete('captcha');
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
115 // Then see if a captcha exists:
Andrey Andreev48a86752012-11-08 15:16:34 +0200116 $sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?';
Derek Jones8ede1a22011-10-05 13:34:52 -0500117 $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
118 $query = $this->db->query($sql, $binds);
119 $row = $query->row();
120
121 if ($row->count == 0)
122 {     
Andrey Andreev48a86752012-11-08 15:16:34 +0200123 echo 'You must submit the word that appears in the image.';
Derek Jones333ad2e2013-07-19 17:03:25 -0700124 }
125
126Available Functions
127===================
128
129The following functions are available:
130
131.. function:: create_captcha([$data = ''[, $img_path = ''[, $img_url = ''[, $font_path = '']]]])
132
133 :param array $data: Array of data for the CAPTCHA
134 :param string $img_path: Path to create the image in
135 :param string $img_url: URL to the CAPTCHA image folder
136 :param string $font_path: Server path to font
137 :returns: array('word' => $word, 'time' => $now, 'image' => $img)
Andrey Andreev3de130c2014-02-07 23:31:49 +0200138 :rtype: array
Derek Jones333ad2e2013-07-19 17:03:25 -0700139
140 Takes an array of information to generate the CAPTCHA as input and
141 creates the image to your specifications, returning an array of
142 associative data about the image.
143
144 ::
145
146 array(
147 'image' => IMAGE TAG
148 'time' => TIMESTAMP (in microtime)
149 'word' => CAPTCHA WORD
150 )
151
152 The **image** is the actual image tag::
153
154 <img src="http://example.com/captcha/12345.jpg" width="140" height="50" />
155
156 The **time** is the micro timestamp used as the image name without the
157 file extension. It will be a number like this: 1139612155.3422
158
159 The **word** is the word that appears in the captcha image, which if not
160 supplied to the function, will be a random string.