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 |
| 14 | |
| 15 | :: |
| 16 | |
| 17 | $this->load->helper('captcha'); |
| 18 | |
| 19 | The following functions are available: |
| 20 | |
| 21 | create_captcha($data) |
| 22 | ===================== |
| 23 | |
| 24 | Takes an array of information to generate the CAPTCHA as input and |
| 25 | creates the image to your specifications, returning an array of |
| 26 | associative data about the image. |
| 27 | |
| 28 | .. php:method:: function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '') |
| 29 | |
| 30 | :param array $data: array of data for the CAPTCHA |
| 31 | :param string $img_path: path to create the image in |
| 32 | :param string $img_url: URL to the CAPTCHA image folder |
| 33 | :param string $font_path: server path to font |
| 34 | :returns: array('word' => $word, 'time' => $now, 'image' => $img) |
| 35 | |
| 36 | |
| 37 | :: |
| 38 | |
| 39 | [array] ( |
| 40 | 'image' => IMAGE TAG |
| 41 | 'time' => TIMESTAMP (in microtime) |
| 42 | 'word' => CAPTCHA WORD ) |
| 43 | |
| 44 | The "image" is the actual image tag: |
| 45 | |
| 46 | :: |
| 47 | |
| 48 | <img src="http://example.com/captcha/12345.jpg" width="140" height="50" /> |
| 49 | |
| 50 | |
| 51 | The "time" is the micro timestamp used as the image name without the |
| 52 | file extension. It will be a number like this: 1139612155.3422 |
| 53 | |
| 54 | The "word" is the word that appears in the captcha image, which if not |
| 55 | supplied to the function, will be a random string. |
| 56 | |
| 57 | Using the CAPTCHA helper |
| 58 | ------------------------ |
| 59 | |
| 60 | Once loaded you can generate a captcha like this |
| 61 | |
| 62 | :: |
| 63 | |
| 64 | $vals = array( |
| 65 | 'word' => 'Random word', |
| 66 | 'img_path' => './captcha/', |
| 67 | 'img_url' => 'http://example.com/captcha/', |
| 68 | 'font_path' => './path/to/fonts/texb.ttf', |
| 69 | 'img_width' => '150', |
| 70 | 'img_height' => 30, |
| 71 | 'expiration' => 7200 |
| 72 | ); |
| 73 | |
| 74 | $cap = create_captcha($vals); echo $cap['image']; |
| 75 | |
| 76 | |
| 77 | - The captcha function requires the GD image library. |
| 78 | - Only the img_path and img_url are required. |
| 79 | - If a "word" is not supplied, the function will generate a random |
| 80 | ASCII string. You might put together your own word library that you |
| 81 | can draw randomly from. |
| 82 | - If you do not specify a path to a TRUE TYPE font, the native ugly GD |
| 83 | font will be used. |
| 84 | - The "captcha" folder must be writable (666, or 777) |
| 85 | - The "expiration" (in seconds) signifies how long an image will remain |
| 86 | in the captcha folder before it will be deleted. The default is two |
| 87 | hours. |
| 88 | |
| 89 | Adding a Database |
| 90 | ----------------- |
| 91 | |
| 92 | In order for the captcha function to prevent someone from submitting, |
| 93 | you will need to add the information returned from create_captcha() |
| 94 | function to your database. Then, when the data from the form is |
| 95 | submitted by the user you will need to verify that the data exists in |
| 96 | the database and has not expired. |
| 97 | |
| 98 | Here is a table prototype |
| 99 | |
| 100 | :: |
| 101 | |
| 102 | CREATE TABLE captcha ( |
| 103 | captcha_id bigint(13) unsigned NOT NULL auto_increment, |
| 104 | captcha_time int(10) unsigned NOT NULL, |
| 105 | ip_address varchar(16) default '0' NOT NULL, |
| 106 | word varchar(20) NOT NULL, |
| 107 | PRIMARY KEY `captcha_id` (`captcha_id`), |
| 108 | KEY `word` (`word`) |
| 109 | ); |
| 110 | |
| 111 | Here is an example of usage with a database. On the page where the |
| 112 | CAPTCHA will be shown you'll have something like this |
| 113 | |
| 114 | :: |
| 115 | |
| 116 | $this->load->helper('captcha'); |
| 117 | $vals = array( |
| 118 | 'img_path' => './captcha/', |
| 119 | 'img_url' => 'http://example.com/captcha/' |
| 120 | ); |
| 121 | |
| 122 | $cap = create_captcha($vals); |
| 123 | $data = array( |
| 124 | 'captcha_time' => $cap['time'], |
| 125 | 'ip_address' => $this->input->ip_address(), |
| 126 | 'word' => $cap['word'] |
| 127 | ); |
| 128 | |
| 129 | $query = $this->db->insert_string('captcha', $data); |
| 130 | $this->db->query($query); |
| 131 | |
| 132 | echo 'Submit the word you see below:'; |
| 133 | echo $cap['image']; |
| 134 | echo '<input type="text" name="captcha" value="" />'; |
| 135 | |
| 136 | Then, on the page that accepts the submission you'll have something like |
| 137 | this |
| 138 | |
| 139 | :: |
| 140 | |
| 141 | // First, delete old captchas |
| 142 | $expiration = time() - 7200; // Two hour limit |
| 143 | $this->db->where('captcha_time < ', $expiration) |
| 144 | ->delete('captcha'); |
| 145 | |
| 146 | // Then see if a captcha exists: |
| 147 | $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?"; |
| 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 | { |
| 154 | echo "You must submit the word that appears in the image"; |
| 155 | } |
| 156 | |