blob: 17462a8de71301c04e03c44ff3f1195023ddd783 [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,
65 'expiration' => 7200
Derek Jones8ede1a22011-10-05 13:34:52 -050066 );
67
Andrey Andreev48a86752012-11-08 15:16:34 +020068 $cap = create_captcha($vals);
69 echo $cap['image'];
Derek Jones8ede1a22011-10-05 13:34:52 -050070
71- The captcha function requires the GD image library.
Andrey Andreev48a86752012-11-08 15:16:34 +020072- Only the **img_path** and **img_url** are required.
73- If a **word** is not supplied, the function will generate a random
Derek Jones8ede1a22011-10-05 13:34:52 -050074 ASCII string. You might put together your own word library that you
75 can draw randomly from.
76- If you do not specify a path to a TRUE TYPE font, the native ugly GD
77 font will be used.
78- The "captcha" folder must be writable (666, or 777)
Andrey Andreev48a86752012-11-08 15:16:34 +020079- The **expiration** (in seconds) signifies how long an image will remain
Derek Jones8ede1a22011-10-05 13:34:52 -050080 in the captcha folder before it will be deleted. The default is two
81 hours.
82
83Adding a Database
84-----------------
85
86In order for the captcha function to prevent someone from submitting,
Andrey Andreev48a86752012-11-08 15:16:34 +020087you will need to add the information returned from ``create_captcha()``
88to your database. Then, when the data from the form is submitted by
89the user you will need to verify that the data exists in the database
90and has not expired.
Derek Jones8ede1a22011-10-05 13:34:52 -050091
Andrey Andreev48a86752012-11-08 15:16:34 +020092Here is a table prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -050093
94 CREATE TABLE captcha (  
95 captcha_id bigint(13) unsigned NOT NULL auto_increment,  
96 captcha_time int(10) unsigned NOT NULL,  
Bo-Yi Wuf3fddf62012-08-31 10:10:16 +080097 ip_address varchar(45) NOT NULL,  
Derek Jones8ede1a22011-10-05 13:34:52 -050098 word varchar(20) NOT NULL,  
99 PRIMARY KEY `captcha_id` (`captcha_id`),  
100 KEY `word` (`word`)
101 );
102
103Here is an example of usage with a database. On the page where the
Andrey Andreev48a86752012-11-08 15:16:34 +0200104CAPTCHA will be shown you'll have something like this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
106 $this->load->helper('captcha');
107 $vals = array(     
108 'img_path' => './captcha/',     
109 'img_url' => 'http://example.com/captcha/'     
110 );
111
112 $cap = create_captcha($vals);
113 $data = array(     
114 'captcha_time' => $cap['time'],     
115 'ip_address' => $this->input->ip_address(),     
116 'word' => $cap['word']     
117 );
118
119 $query = $this->db->insert_string('captcha', $data);
120 $this->db->query($query);
121
122 echo 'Submit the word you see below:';
123 echo $cap['image'];
124 echo '<input type="text" name="captcha" value="" />';
125
126Then, on the page that accepts the submission you'll have something like
Andrey Andreev48a86752012-11-08 15:16:34 +0200127this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
129 // First, delete old captchas
130 $expiration = time() - 7200; // Two hour limit
131 $this->db->where('captcha_time < ', $expiration)
Andrey Andreev48a86752012-11-08 15:16:34 +0200132 ->delete('captcha');
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
134 // Then see if a captcha exists:
Andrey Andreev48a86752012-11-08 15:16:34 +0200135 $sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?';
Derek Jones8ede1a22011-10-05 13:34:52 -0500136 $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
137 $query = $this->db->query($sql, $binds);
138 $row = $query->row();
139
140 if ($row->count == 0)
141 {     
Andrey Andreev48a86752012-11-08 15:16:34 +0200142 echo 'You must submit the word that appears in the image.';
143 }