Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 3 | <head> |
| 4 | |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 6 | <title>CAPTCHA Helper : CodeIgniter User Guide</title> |
| 7 | |
| 8 | <style type='text/css' media='all'>@import url('../userguide.css');</style> |
| 9 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> |
| 10 | |
| 11 | <script type="text/javascript" src="../nav/nav.js"></script> |
| 12 | <script type="text/javascript" src="../nav/prototype.lite.js"></script> |
| 13 | <script type="text/javascript" src="../nav/moo.fx.js"></script> |
| 14 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script> |
| 15 | |
| 16 | <meta http-equiv='expires' content='-1' /> |
| 17 | <meta http-equiv= 'pragma' content='no-cache' /> |
| 18 | <meta name='robots' content='all' /> |
| 19 | <meta name='author' content='ExpressionEngine Dev Team' /> |
| 20 | <meta name='description' content='CodeIgniter User Guide' /> |
| 21 | |
| 22 | </head> |
| 23 | <body> |
| 24 | |
| 25 | <!-- START NAVIGATION --> |
| 26 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> |
| 27 | <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> |
| 28 | <div id="masthead"> |
| 29 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%"> |
| 30 | <tr> |
Pascal Kriete | 1f62229 | 2011-04-07 12:06:51 -0400 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 2.0.2</h1></td> |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> |
| 33 | </tr> |
| 34 | </table> |
| 35 | </div> |
| 36 | <!-- END NAVIGATION --> |
| 37 | |
| 38 | |
| 39 | <!-- START BREADCRUMB --> |
| 40 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%"> |
| 41 | <tr> |
| 42 | <td id="breadcrumb"> |
| 43 | <a href="http://codeigniter.com/">CodeIgniter Home</a> › |
| 44 | <a href="../index.html">User Guide Home</a> › |
| 45 | CAPTCHA Helper |
| 46 | </td> |
| 47 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> |
| 48 | </tr> |
| 49 | </table> |
| 50 | <!-- END BREADCRUMB --> |
| 51 | |
| 52 | <br clear="all" /> |
| 53 | |
| 54 | |
| 55 | <!-- START CONTENT --> |
| 56 | <div id="content"> |
| 57 | |
| 58 | |
| 59 | <h1>CAPTCHA Helper</h1> |
| 60 | |
| 61 | <p>The CAPTCHA Helper file contains functions that assist in creating CAPTCHA images.</p> |
| 62 | |
| 63 | |
| 64 | <h2>Loading this Helper</h2> |
| 65 | |
| 66 | <p>This helper is loaded using the following code:</p> |
| 67 | <code>$this->load->helper('captcha');</code> |
| 68 | |
| 69 | <p>The following functions are available:</p> |
| 70 | |
Eric Barnes | a170cc1 | 2011-03-10 20:39:54 -0500 | [diff] [blame] | 71 | <h2>create_captcha(<var>$data</var>)</h2> |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 72 | |
| 73 | <p>Takes an array of information to generate the CAPTCHA as input and creates the image to your specifications, returning an array of associative data about the image.</p> |
| 74 | |
| 75 | <code>[array]<br /> |
| 76 | (<br /> |
| 77 | 'image' => IMAGE TAG<br /> |
| 78 | 'time' => TIMESTAMP (in microtime)<br /> |
| 79 | 'word' => CAPTCHA WORD<br /> |
| 80 | )</code> |
| 81 | |
| 82 | <p>The "image" is the actual image tag: |
| 83 | <code><img src="http://example.com/captcha/12345.jpg" width="140" height="50" /></code></p> |
| 84 | |
| 85 | <p>The "time" is the micro timestamp used as the image name without the file |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame^] | 86 | extension. It will be a number like this: 1139612155.3422</p> |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 87 | |
| 88 | <p>The "word" is the word that appears in the captcha image, which if not |
| 89 | supplied to the function, will be a random string.</p> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | |
Derek Jones | c6da503 | 2010-03-09 20:44:27 -0600 | [diff] [blame] | 91 | <h3>Using the CAPTCHA helper</h3> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 93 | <p>Once loaded you can generate a captcha like this:</p> |
| 94 | |
| 95 | <code>$vals = array(<br /> |
| 96 | 'word' => 'Random word',<br /> |
| 97 | 'img_path' => './captcha/',<br /> |
| 98 | 'img_url' => 'http://example.com/captcha/',<br /> |
| 99 | 'font_path' => './path/to/fonts/texb.ttf',<br /> |
| 100 | 'img_width' => '150',<br /> |
| 101 | 'img_height' => 30,<br /> |
| 102 | 'expiration' => 7200<br /> |
| 103 | );<br /> |
| 104 | <br /> |
| 105 | $cap = create_captcha($vals);<br /> |
| 106 | echo $cap['image'];</code> |
| 107 | |
| 108 | <ul> |
| 109 | <li>The captcha function requires the GD image library.</li> |
| 110 | <li>Only the img_path and img_url are required.</li> |
| 111 | <li>If a "word" is not supplied, the function will generate a random |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame^] | 112 | ASCII string. You might put together your own word library that |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 113 | you can draw randomly from.</li> |
| 114 | <li>If you do not specify a path to a TRUE TYPE font, the native ugly GD |
| 115 | font will be used.</li> |
| 116 | <li>The "captcha" folder must be writable (666, or 777)</li> |
| 117 | <li>The "expiration" (in seconds) signifies how long an image will |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame^] | 118 | remain in the captcha folder before it will be deleted. The default |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 119 | is two hours.</li> |
| 120 | </ul> |
| 121 | |
| 122 | <h3>Adding a Database</h3> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 123 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 124 | <p>In order for the captcha function to prevent someone from submitting, you will need |
| 125 | to add the information returned from <kbd>create_captcha()</kbd> function to your database. |
| 126 | Then, when the data from the form is submitted by the user you will need to verify |
| 127 | that the data exists in the database and has not expired.</p> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 128 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 129 | <p>Here is a table prototype:</p> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 130 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 131 | <code>CREATE TABLE captcha (<br /> |
| 132 | captcha_id bigint(13) unsigned NOT NULL auto_increment,<br /> |
| 133 | captcha_time int(10) unsigned NOT NULL,<br /> |
| 134 | ip_address varchar(16) default '0' NOT NULL,<br /> |
| 135 | word varchar(20) NOT NULL,<br /> |
| 136 | PRIMARY KEY `captcha_id` (`captcha_id`),<br /> |
| 137 | KEY `word` (`word`)<br /> |
Greg Aker | e1f6e9d | 2010-11-01 12:24:00 -0500 | [diff] [blame] | 138 | );</code> |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 139 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame^] | 140 | <p>Here is an example of usage with a database. On the page where the CAPTCHA will be shown you'll have something like this:</p> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 141 | |
Derek Jones | c6da503 | 2010-03-09 20:44:27 -0600 | [diff] [blame] | 142 | <code>$this->load->helper('captcha');<br /> |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 143 | $vals = array(<br /> |
| 144 | 'img_path' => './captcha/',<br /> |
| 145 | 'img_url' => 'http://example.com/captcha/'<br /> |
| 146 | );<br /> |
| 147 | <br /> |
| 148 | $cap = create_captcha($vals);<br /> |
| 149 | <br /> |
| 150 | $data = array(<br /> |
| 151 | 'captcha_time' => $cap['time'],<br /> |
| 152 | 'ip_address' => $this->input->ip_address(),<br /> |
| 153 | 'word' => $cap['word']<br /> |
| 154 | );<br /> |
| 155 | <br /> |
| 156 | $query = $this->db->insert_string('captcha', $data);<br /> |
| 157 | $this->db->query($query);<br /> |
| 158 | <br /> |
| 159 | echo 'Submit the word you see below:';<br /> |
| 160 | echo $cap['image'];<br /> |
| 161 | echo '<input type="text" name="captcha" value="" />';</code> |
| 162 | |
| 163 | <p>Then, on the page that accepts the submission you'll have something like this:</p> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 165 | <code>// First, delete old captchas<br /> |
| 166 | $expiration = time()-7200; // Two hour limit<br /> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 167 | $this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration); <br /> |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 168 | <br /> |
| 169 | // Then see if a captcha exists:<br /> |
Phil Sturgeon | 8fd08a1 | 2011-04-02 12:26:38 +0100 | [diff] [blame] | 170 | $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";<br /> |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 171 | $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);<br /> |
| 172 | $query = $this->db->query($sql, $binds);<br /> |
| 173 | $row = $query->row();<br /> |
| 174 | <br /> |
| 175 | if ($row->count == 0)<br /> |
| 176 | {<br /> |
| 177 | echo "You must submit the word that appears in the image";<br /> |
| 178 | }</code> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 180 | </div> |
| 181 | <!-- END CONTENT --> |
| 182 | |
| 183 | |
| 184 | <div id="footer"> |
| 185 | <p> |
| 186 | Previous Topic: <a href="url_helper.html">URL Helper</a> |
| 187 | · |
| 188 | <a href="#top">Top of Page</a> · |
| 189 | <a href="../index.html">User Guide Home</a> |
| 190 | </p> |
Derek Jones | 898949f | 2011-01-28 07:42:16 -0600 | [diff] [blame] | 191 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 192 | </div> |
| 193 | |
| 194 | </body> |
Greg Aker | e1f6e9d | 2010-11-01 12:24:00 -0500 | [diff] [blame] | 195 | </html> |