blob: 3c56addf336ab1c1176cf3bf5613dd1434a97d4b [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
29 :returns: array('word' => $word, 'time' => $now, 'image' => $img)
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(
38 'image' => IMAGE TAG
39 'time' => TIMESTAMP (in microtime)
40 'word' => CAPTCHA WORD
41 )
Derek Jones8ede1a22011-10-05 13:34:52 -050042
Andrey Andreev48a86752012-11-08 15:16:34 +020043The **image** is the actual image tag::
Derek Jones8ede1a22011-10-05 13:34:52 -050044
45 <img src="http://example.com/captcha/12345.jpg" width="140" height="50" />
46
Andrey Andreev48a86752012-11-08 15:16:34 +020047The **time** is the micro timestamp used as the image name without the
Derek Jones8ede1a22011-10-05 13:34:52 -050048file extension. It will be a number like this: 1139612155.3422
49
Andrey Andreev48a86752012-11-08 15:16:34 +020050The **word** is the word that appears in the captcha image, which if not
Derek Jones8ede1a22011-10-05 13:34:52 -050051supplied to the function, will be a random string.
52
53Using the CAPTCHA helper
54------------------------
55
Andrey Andreev48a86752012-11-08 15:16:34 +020056Once loaded you can generate a captcha like this::
Derek Jones8ede1a22011-10-05 13:34:52 -050057
Andrey Andreev48a86752012-11-08 15:16:34 +020058 $vals = array(
59 'word' => 'Random word',
60 'img_path' => './captcha/',
61 'img_url' => 'http://example.com/captcha/',
62 'font_path' => './path/to/fonts/texb.ttf',
63 'img_width' => '150',
64 'img_height' => 30,
ash3fd9bf82013-04-10 12:40:31 +010065 'expiration' => 7200,
ash29ae72d2013-04-10 13:59:42 +010066 'word_length' => 8,
Andrey Andreev8963f402013-07-18 16:02:47 +030067 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
68
Andrey Andreevc0a1ce92013-07-18 16:05:02 +030069 // White background and border, black text and red grid
Andrey Andreev8963f402013-07-18 16:02:47 +030070 'colors' => array(
71 'background' => array(255, 255, 255),
72 'border' => array(255, 255, 255),
73 'text' => array(0, 0, 0),
74 'grid' => array(255, 40, 40)
75 )
Derek Jones8ede1a22011-10-05 13:34:52 -050076 );
77
Andrey Andreev48a86752012-11-08 15:16:34 +020078 $cap = create_captcha($vals);
79 echo $cap['image'];
Derek Jones8ede1a22011-10-05 13:34:52 -050080
81- The captcha function requires the GD image library.
Andrey Andreev48a86752012-11-08 15:16:34 +020082- Only the **img_path** and **img_url** are required.
83- If a **word** is not supplied, the function will generate a random
Derek Jones8ede1a22011-10-05 13:34:52 -050084 ASCII string. You might put together your own word library that you
85 can draw randomly from.
86- If you do not specify a path to a TRUE TYPE font, the native ugly GD
87 font will be used.
88- The "captcha" folder must be writable (666, or 777)
Andrey Andreev48a86752012-11-08 15:16:34 +020089- The **expiration** (in seconds) signifies how long an image will remain
Derek Jones8ede1a22011-10-05 13:34:52 -050090 in the captcha folder before it will be deleted. The default is two
91 hours.
ash29ae72d2013-04-10 13:59:42 +010092- **word_length** defaults to 8, **pool** defaults to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
Andrey Andreev8963f402013-07-18 16:02:47 +030093- If any of the **colors** values is missing, it will be replaced by the default.
Derek Jones8ede1a22011-10-05 13:34:52 -050094
95Adding a Database
96-----------------
97
98In order for the captcha function to prevent someone from submitting,
Andrey Andreev48a86752012-11-08 15:16:34 +020099you will need to add the information returned from ``create_captcha()``
100to your database. Then, when the data from the form is submitted by
101the user you will need to verify that the data exists in the database
102and has not expired.
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
Andrey Andreev48a86752012-11-08 15:16:34 +0200104Here is a table prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
106 CREATE TABLE captcha (  
107 captcha_id bigint(13) unsigned NOT NULL auto_increment,  
108 captcha_time int(10) unsigned NOT NULL,  
Bo-Yi Wuf3fddf62012-08-31 10:10:16 +0800109 ip_address varchar(45) NOT NULL,  
Derek Jones8ede1a22011-10-05 13:34:52 -0500110 word varchar(20) NOT NULL,  
111 PRIMARY KEY `captcha_id` (`captcha_id`),  
112 KEY `word` (`word`)
113 );
114
115Here is an example of usage with a database. On the page where the
Andrey Andreev48a86752012-11-08 15:16:34 +0200116CAPTCHA will be shown you'll have something like this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500117
118 $this->load->helper('captcha');
119 $vals = array(     
120 'img_path' => './captcha/',     
121 'img_url' => 'http://example.com/captcha/'     
122 );
123
124 $cap = create_captcha($vals);
125 $data = array(     
126 'captcha_time' => $cap['time'],     
127 'ip_address' => $this->input->ip_address(),     
128 'word' => $cap['word']     
129 );
130
131 $query = $this->db->insert_string('captcha', $data);
132 $this->db->query($query);
133
134 echo 'Submit the word you see below:';
135 echo $cap['image'];
136 echo '<input type="text" name="captcha" value="" />';
137
138Then, on the page that accepts the submission you'll have something like
Andrey Andreev48a86752012-11-08 15:16:34 +0200139this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
141 // First, delete old captchas
142 $expiration = time() - 7200; // Two hour limit
143 $this->db->where('captcha_time < ', $expiration)
Andrey Andreev48a86752012-11-08 15:16:34 +0200144 ->delete('captcha');
Derek Jones8ede1a22011-10-05 13:34:52 -0500145
146 // Then see if a captcha exists:
Andrey Andreev48a86752012-11-08 15:16:34 +0200147 $sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?';
Derek Jones8ede1a22011-10-05 13:34:52 -0500148 $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
149 $query = $this->db->query($sql, $binds);
150 $row = $query->row();
151
152 if ($row->count == 0)
153 {     
Andrey Andreev48a86752012-11-08 15:16:34 +0200154 echo 'You must submit the word that appears in the image.';
ash3a66fac2013-04-14 14:38:03 +0100155 }