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 | |
Derek Jones | 333ad2e | 2013-07-19 17:03:25 -0700 | [diff] [blame] | 8 | .. contents:: |
| 9 | :local: |
| 10 | |
| 11 | .. raw:: html |
| 12 | |
| 13 | <div class="custom-index container"></div> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 14 | |
| 15 | Loading this Helper |
| 16 | =================== |
| 17 | |
Derek Jones | 333ad2e | 2013-07-19 17:03:25 -0700 | [diff] [blame] | 18 | This helper is loaded using the following code:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 19 | |
| 20 | $this->load->helper('captcha'); |
| 21 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 22 | Using the CAPTCHA helper |
Derek Jones | 333ad2e | 2013-07-19 17:03:25 -0700 | [diff] [blame] | 23 | ======================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 24 | |
Derek Jones | 333ad2e | 2013-07-19 17:03:25 -0700 | [diff] [blame] | 25 | Once loaded you can generate a CAPTCHA like this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 26 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 27 | $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, |
ash | 3fd9bf8 | 2013-04-10 12:40:31 +0100 | [diff] [blame] | 34 | 'expiration' => 7200, |
ash | 29ae72d | 2013-04-10 13:59:42 +0100 | [diff] [blame] | 35 | 'word_length' => 8, |
Andrey Andreev | 8963f40 | 2013-07-18 16:02:47 +0300 | [diff] [blame] | 36 | 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 37 | |
Andrey Andreev | c0a1ce9 | 2013-07-18 16:05:02 +0300 | [diff] [blame] | 38 | // White background and border, black text and red grid |
Andrey Andreev | 8963f40 | 2013-07-18 16:02:47 +0300 | [diff] [blame] | 39 | '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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 45 | ); |
| 46 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 47 | $cap = create_captcha($vals); |
| 48 | echo $cap['image']; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 49 | |
| 50 | - The captcha function requires the GD image library. |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 51 | - Only the **img_path** and **img_url** are required. |
| 52 | - If a **word** is not supplied, the function will generate a random |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | 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 Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 58 | - The **expiration** (in seconds) signifies how long an image will remain |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 59 | in the captcha folder before it will be deleted. The default is two |
| 60 | hours. |
ash | 29ae72d | 2013-04-10 13:59:42 +0100 | [diff] [blame] | 61 | - **word_length** defaults to 8, **pool** defaults to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' |
Andrey Andreev | 8963f40 | 2013-07-18 16:02:47 +0300 | [diff] [blame] | 62 | - 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] | 63 | |
| 64 | Adding a Database |
| 65 | ----------------- |
| 66 | |
| 67 | In order for the captcha function to prevent someone from submitting, |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 68 | you will need to add the information returned from ``create_captcha()`` |
| 69 | to your database. Then, when the data from the form is submitted by |
| 70 | the user you will need to verify that the data exists in the database |
| 71 | and has not expired. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 72 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 73 | Here is a table prototype:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
| 75 | CREATE TABLE captcha ( |
| 76 | captcha_id bigint(13) unsigned NOT NULL auto_increment, |
| 77 | captcha_time int(10) unsigned NOT NULL, |
Bo-Yi Wu | f3fddf6 | 2012-08-31 10:10:16 +0800 | [diff] [blame] | 78 | ip_address varchar(45) NOT NULL, |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 79 | word varchar(20) NOT NULL, |
| 80 | PRIMARY KEY `captcha_id` (`captcha_id`), |
| 81 | KEY `word` (`word`) |
| 82 | ); |
| 83 | |
| 84 | 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] | 85 | CAPTCHA will be shown you'll have something like this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 86 | |
| 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 Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 104 | echo $cap['image']; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 105 | echo '<input type="text" name="captcha" value="" />'; |
| 106 | |
| 107 | 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] | 108 | this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 109 | |
| 110 | // First, delete old captchas |
| 111 | $expiration = time() - 7200; // Two hour limit |
| 112 | $this->db->where('captcha_time < ', $expiration) |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 113 | ->delete('captcha'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
| 115 | // Then see if a captcha exists: |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 116 | $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] | 117 | $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 Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 123 | echo 'You must submit the word that appears in the image.'; |
Derek Jones | 333ad2e | 2013-07-19 17:03:25 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | Available Functions |
| 127 | =================== |
| 128 | |
| 129 | The 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) |
| 138 | |
| 139 | Takes an array of information to generate the CAPTCHA as input and |
| 140 | creates the image to your specifications, returning an array of |
| 141 | associative data about the image. |
| 142 | |
| 143 | :: |
| 144 | |
| 145 | array( |
| 146 | 'image' => IMAGE TAG |
| 147 | 'time' => TIMESTAMP (in microtime) |
| 148 | 'word' => CAPTCHA WORD |
| 149 | ) |
| 150 | |
| 151 | The **image** is the actual image tag:: |
| 152 | |
| 153 | <img src="http://example.com/captcha/12345.jpg" width="140" height="50" /> |
| 154 | |
| 155 | The **time** is the micro timestamp used as the image name without the |
| 156 | file extension. It will be a number like this: 1139612155.3422 |
| 157 | |
| 158 | The **word** is the word that appears in the captcha image, which if not |
| 159 | supplied to the function, will be a random string. |