Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############## |
| 2 | CAPTCHA Helper |
| 3 | ############## |
| 4 | |
| 5 | The CAPTCHA Helper file contains functions that assist in creating |
| 6 | CAPTCHA images. |
| 7 | |
| 8 | .. contents:: Page Contents |
| 9 | |
| 10 | Loading this Helper |
| 11 | =================== |
| 12 | |
| 13 | This helper is loaded using the following code |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 14 | :: |
| 15 | |
| 16 | $this->load->helper('captcha'); |
| 17 | |
| 18 | The following functions are available: |
| 19 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 20 | create_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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | |
| 31 | Takes an array of information to generate the CAPTCHA as input and |
| 32 | creates the image to your specifications, returning an array of |
| 33 | associative data about the image. |
| 34 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | :: |
| 36 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 37 | array( |
| 38 | 'image' => IMAGE TAG |
| 39 | 'time' => TIMESTAMP (in microtime) |
| 40 | 'word' => CAPTCHA WORD |
| 41 | ) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 42 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 43 | The **image** is the actual image tag:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 44 | |
| 45 | <img src="http://example.com/captcha/12345.jpg" width="140" height="50" /> |
| 46 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 47 | The **time** is the micro timestamp used as the image name without the |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 48 | file extension. It will be a number like this: 1139612155.3422 |
| 49 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 50 | The **word** is the word that appears in the captcha image, which if not |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | supplied to the function, will be a random string. |
| 52 | |
| 53 | Using the CAPTCHA helper |
| 54 | ------------------------ |
| 55 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 56 | Once loaded you can generate a captcha like this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 57 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 58 | $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, |
ash | 3fd9bf8 | 2013-04-10 12:40:31 +0100 | [diff] [blame] | 65 | 'expiration' => 7200, |
ash | 29ae72d | 2013-04-10 13:59:42 +0100 | [diff] [blame] | 66 | 'word_length' => 8, |
Andrey Andreev | 8963f40 | 2013-07-18 16:02:47 +0300 | [diff] [blame] | 67 | 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 68 | |
Andrey Andreev | c0a1ce9 | 2013-07-18 16:05:02 +0300 | [diff] [blame] | 69 | // White background and border, black text and red grid |
Andrey Andreev | 8963f40 | 2013-07-18 16:02:47 +0300 | [diff] [blame] | 70 | '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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 76 | ); |
| 77 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 78 | $cap = create_captcha($vals); |
| 79 | echo $cap['image']; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | |
| 81 | - The captcha function requires the GD image library. |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 82 | - Only the **img_path** and **img_url** are required. |
| 83 | - If a **word** is not supplied, the function will generate a random |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | 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 Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 89 | - The **expiration** (in seconds) signifies how long an image will remain |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 90 | in the captcha folder before it will be deleted. The default is two |
| 91 | hours. |
ash | 29ae72d | 2013-04-10 13:59:42 +0100 | [diff] [blame] | 92 | - **word_length** defaults to 8, **pool** defaults to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' |
Andrey Andreev | 8963f40 | 2013-07-18 16:02:47 +0300 | [diff] [blame] | 93 | - If any of the **colors** values is missing, it will be replaced by the default. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 94 | |
| 95 | Adding a Database |
| 96 | ----------------- |
| 97 | |
| 98 | In order for the captcha function to prevent someone from submitting, |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 99 | you will need to add the information returned from ``create_captcha()`` |
| 100 | to your database. Then, when the data from the form is submitted by |
| 101 | the user you will need to verify that the data exists in the database |
| 102 | and has not expired. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 103 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 104 | Here is a table prototype:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 105 | |
| 106 | CREATE TABLE captcha ( |
| 107 | captcha_id bigint(13) unsigned NOT NULL auto_increment, |
| 108 | captcha_time int(10) unsigned NOT NULL, |
Bo-Yi Wu | f3fddf6 | 2012-08-31 10:10:16 +0800 | [diff] [blame] | 109 | ip_address varchar(45) NOT NULL, |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | word varchar(20) NOT NULL, |
| 111 | PRIMARY KEY `captcha_id` (`captcha_id`), |
| 112 | KEY `word` (`word`) |
| 113 | ); |
| 114 | |
| 115 | Here is an example of usage with a database. On the page where the |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 116 | CAPTCHA will be shown you'll have something like this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 117 | |
| 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 | |
| 138 | Then, on the page that accepts the submission you'll have something like |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 139 | this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 140 | |
| 141 | // First, delete old captchas |
| 142 | $expiration = time() - 7200; // Two hour limit |
| 143 | $this->db->where('captcha_time < ', $expiration) |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 144 | ->delete('captcha'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 145 | |
| 146 | // Then see if a captcha exists: |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 147 | $sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 148 | $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 Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 154 | echo 'You must submit the word that appears in the image.'; |
ash | 3a66fac | 2013-04-14 14:38:03 +0100 | [diff] [blame] | 155 | } |