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">
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <title>CAPTCHA Helper : CodeIgniter User Guide</title>
8 <style type='text/css' media='all'>@import url('../userguide.css');</style>
9 <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
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>
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' />
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>
29 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
31 <td><h1>CodeIgniter User Guide Version 2.0.2</h1></td>
32 <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
36 <!-- END NAVIGATION -->
39 <!-- START BREADCRUMB -->
40 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
43 <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
44 <a href="../index.html">User Guide Home</a> ›
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>
50 <!-- END BREADCRUMB -->
55 <!-- START CONTENT -->
59 <h1>CAPTCHA Helper</h1>
61 <p>The CAPTCHA Helper file contains functions that assist in creating CAPTCHA images.</p>
64 <h2>Loading this Helper</h2>
66 <p>This helper is loaded using the following code:</p>
67 <code>$this->load->helper('captcha');</code>
69 <p>The following functions are available:</p>
71 <h2>create_captcha(<var>$data</var>)</h2>
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>
77 'image' => IMAGE TAG<br />
78 'time' => TIMESTAMP (in microtime)<br />
79 'word' => CAPTCHA WORD<br />
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>
85 <p>The "time" is the micro timestamp used as the image name without the file
86 extension. It will be a number like this: 1139612155.3422</p>
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>
91 <h3>Using the CAPTCHA helper</h3>
93 <p>Once loaded you can generate a captcha like this:</p>
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 />
105 $cap = create_captcha($vals);<br />
106 echo $cap['image'];</code>
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
112 ASCII string. You might put together your own word library that
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
118 remain in the captcha folder before it will be deleted. The default
122 <h3>Adding a Database</h3>
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>
129 <p>Here is a table prototype:</p>
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 />
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>
142 <code>$this->load->helper('captcha');<br />
144 'img_path' => './captcha/',<br />
145 'img_url' => 'http://example.com/captcha/'<br />
146 );<br />
148 $cap = create_captcha($vals);<br />
151 'captcha_time' => $cap['time'],<br />
152 'ip_address' => $this->input->ip_address(),<br />
153 'word' => $cap['word']<br />
154 );<br />
156 $query = $this->db->insert_string('captcha', $data);<br />
157 $this->db->query($query);<br />
159 echo 'Submit the word you see below:';<br />
160 echo $cap['image'];<br />
161 echo '<input type="text" name="captcha" value="" />';</code>
163 <p>Then, on the page that accepts the submission you'll have something like this:</p>
165 <code>// First, delete old captchas<br />
166 $expiration = time()-7200; // Two hour limit<br />
167 $this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration); <br />
169 // Then see if a captcha exists:<br />
170 $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";<br />
171 $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);<br />
172 $query = $this->db->query($sql, $binds);<br />
173 $row = $query->row();<br />
175 if ($row->count == 0)<br />
177 echo "You must submit the word that appears in the image";<br />
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>
191 <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p>