blob: ca24e011fded1ace33d923e81f0b6d9d1c19efc2 [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,
67 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
Derek Jones8ede1a22011-10-05 13:34:52 -050068 );
69
Andrey Andreev48a86752012-11-08 15:16:34 +020070 $cap = create_captcha($vals);
71 echo $cap['image'];
Derek Jones8ede1a22011-10-05 13:34:52 -050072
73- The captcha function requires the GD image library.
Andrey Andreev48a86752012-11-08 15:16:34 +020074- Only the **img_path** and **img_url** are required.
75- If a **word** is not supplied, the function will generate a random
Derek Jones8ede1a22011-10-05 13:34:52 -050076 ASCII string. You might put together your own word library that you
77 can draw randomly from.
78- If you do not specify a path to a TRUE TYPE font, the native ugly GD
79 font will be used.
80- The "captcha" folder must be writable (666, or 777)
Andrey Andreev48a86752012-11-08 15:16:34 +020081- The **expiration** (in seconds) signifies how long an image will remain
Derek Jones8ede1a22011-10-05 13:34:52 -050082 in the captcha folder before it will be deleted. The default is two
83 hours.
ash29ae72d2013-04-10 13:59:42 +010084- **word_length** defaults to 8, **pool** defaults to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
Derek Jones8ede1a22011-10-05 13:34:52 -050085
86Adding a Database
87-----------------
88
89In order for the captcha function to prevent someone from submitting,
Andrey Andreev48a86752012-11-08 15:16:34 +020090you will need to add the information returned from ``create_captcha()``
91to your database. Then, when the data from the form is submitted by
92the user you will need to verify that the data exists in the database
93and has not expired.
Derek Jones8ede1a22011-10-05 13:34:52 -050094
Andrey Andreev48a86752012-11-08 15:16:34 +020095Here is a table prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -050096
97 CREATE TABLE captcha (  
98 captcha_id bigint(13) unsigned NOT NULL auto_increment,  
99 captcha_time int(10) unsigned NOT NULL,  
Bo-Yi Wuf3fddf62012-08-31 10:10:16 +0800100 ip_address varchar(45) NOT NULL,  
Derek Jones8ede1a22011-10-05 13:34:52 -0500101 word varchar(20) NOT NULL,  
102 PRIMARY KEY `captcha_id` (`captcha_id`),  
103 KEY `word` (`word`)
104 );
105
106Here is an example of usage with a database. On the page where the
Andrey Andreev48a86752012-11-08 15:16:34 +0200107CAPTCHA will be shown you'll have something like this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
109 $this->load->helper('captcha');
110 $vals = array(     
111 'img_path' => './captcha/',     
112 'img_url' => 'http://example.com/captcha/'     
113 );
114
115 $cap = create_captcha($vals);
116 $data = array(     
117 'captcha_time' => $cap['time'],     
118 'ip_address' => $this->input->ip_address(),     
119 'word' => $cap['word']     
120 );
121
122 $query = $this->db->insert_string('captcha', $data);
123 $this->db->query($query);
124
125 echo 'Submit the word you see below:';
126 echo $cap['image'];
127 echo '<input type="text" name="captcha" value="" />';
128
129Then, on the page that accepts the submission you'll have something like
Andrey Andreev48a86752012-11-08 15:16:34 +0200130this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
132 // First, delete old captchas
133 $expiration = time() - 7200; // Two hour limit
134 $this->db->where('captcha_time < ', $expiration)
Andrey Andreev48a86752012-11-08 15:16:34 +0200135 ->delete('captcha');
Derek Jones8ede1a22011-10-05 13:34:52 -0500136
137 // Then see if a captcha exists:
Andrey Andreev48a86752012-11-08 15:16:34 +0200138 $sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?';
Derek Jones8ede1a22011-10-05 13:34:52 -0500139 $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
140 $query = $this->db->query($sql, $binds);
141 $row = $query->row();
142
143 if ($row->count == 0)
144 {     
Andrey Andreev48a86752012-11-08 15:16:34 +0200145 echo 'You must submit the word that appears in the image.';
ash3a66fac2013-04-14 14:38:03 +0100146 }