blob: 3c6fa11886a612c94fa87077608f090190970c85 [file] [log] [blame]
Derek Jonesb41032d2010-03-05 11:22:45 -06001<!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 Kriete1f622292011-04-07 12:06:51 -040031<td><h1>CodeIgniter User Guide Version 2.0.2</h1></td>
Derek Jonesb41032d2010-03-05 11:22:45 -060032<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> &nbsp;&#8250;&nbsp;
44<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45CAPTCHA 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&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<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 Barnesa170cc12011-03-10 20:39:54 -050071<h2>create_captcha(<var>$data</var>)</h2>
Derek Jonesb41032d2010-03-05 11:22:45 -060072
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&nbsp;&nbsp;'image' => IMAGE TAG<br />
78&nbsp;&nbsp;'time' => TIMESTAMP (in microtime)<br />
79&nbsp;&nbsp;'word' => CAPTCHA WORD<br />
80)</code>
81
82 <p>The "image" is the actual image tag:
83<code>&lt;img src=&quot;http://example.com/captcha/12345.jpg&quot; width=&quot;140&quot; height=&quot;50&quot; /&gt;</code></p>
84
85 <p>The "time" is the micro timestamp used as the image name without the file
Razican114ab092011-04-25 17:26:45 +020086 extension. It will be a number like this: 1139612155.3422</p>
Derek Jonesb41032d2010-03-05 11:22:45 -060087
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 Mienydd671972010-10-04 16:33:58 +020090
Derek Jonesc6da5032010-03-09 20:44:27 -060091 <h3>Using the CAPTCHA helper</h3>
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Jonesb41032d2010-03-05 11:22:45 -060093 <p>Once loaded you can generate a captcha like this:</p>
94
95<code>$vals = array(<br />
96&nbsp;&nbsp;&nbsp;&nbsp;'word' => 'Random word',<br />
97&nbsp;&nbsp;&nbsp;&nbsp;'img_path' => './captcha/',<br />
98&nbsp;&nbsp;&nbsp;&nbsp;'img_url' => 'http://example.com/captcha/',<br />
99&nbsp;&nbsp;&nbsp;&nbsp;'font_path' => './path/to/fonts/texb.ttf',<br />
100&nbsp;&nbsp;&nbsp;&nbsp;'img_width' => '150',<br />
101&nbsp;&nbsp;&nbsp;&nbsp;'img_height' => 30,<br />
102&nbsp;&nbsp;&nbsp;&nbsp;'expiration' => 7200<br />
103&nbsp;&nbsp;&nbsp;&nbsp;);<br />
104<br />
105$cap = create_captcha($vals);<br />
106echo $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
Razican114ab092011-04-25 17:26:45 +0200112 ASCII string. You might put together your own word library that
Derek Jonesb41032d2010-03-05 11:22:45 -0600113 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
Razican114ab092011-04-25 17:26:45 +0200118 remain in the captcha folder before it will be deleted. The default
Derek Jonesb41032d2010-03-05 11:22:45 -0600119 is two hours.</li>
120 </ul>
121
122 <h3>Adding a Database</h3>
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Jonesb41032d2010-03-05 11:22:45 -0600124 <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 Mienydd671972010-10-04 16:33:58 +0200128
Derek Jonesb41032d2010-03-05 11:22:45 -0600129 <p>Here is a table prototype:</p>
Barry Mienydd671972010-10-04 16:33:58 +0200130
Derek Jonesb41032d2010-03-05 11:22:45 -0600131<code>CREATE TABLE captcha (<br />
132&nbsp;captcha_id bigint(13) unsigned NOT NULL auto_increment,<br />
133&nbsp;captcha_time int(10) unsigned NOT NULL,<br />
134&nbsp;ip_address varchar(16) default '0' NOT NULL,<br />
135&nbsp;word varchar(20) NOT NULL,<br />
136&nbsp;PRIMARY KEY `captcha_id` (`captcha_id`),<br />
137&nbsp;KEY `word` (`word`)<br />
Greg Akere1f6e9d2010-11-01 12:24:00 -0500138);</code>
Derek Jonesb41032d2010-03-05 11:22:45 -0600139
Razican114ab092011-04-25 17:26:45 +0200140 <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 Mienydd671972010-10-04 16:33:58 +0200141
Derek Jonesc6da5032010-03-09 20:44:27 -0600142<code>$this-&gt;load-&gt;helper(&#x27;captcha&#x27;);<br />
Derek Jonesb41032d2010-03-05 11:22:45 -0600143$vals = array(<br />
144&nbsp;&nbsp;&nbsp;&nbsp;&#x27;img_path&#x27; =&gt; &#x27;./captcha/&#x27;,<br />
145&nbsp;&nbsp;&nbsp;&nbsp;&#x27;img_url&#x27; =&gt; &#x27;http://example.com/captcha/&#x27;<br />
146&nbsp;&nbsp;&nbsp;&nbsp;);<br />
147<br />
148$cap = create_captcha($vals);<br />
149<br />
150$data = array(<br />
151&nbsp;&nbsp;&nbsp;&nbsp;&#x27;captcha_time&#x27; =&gt; $cap[&#x27;time&#x27;],<br />
152&nbsp;&nbsp;&nbsp;&nbsp;&#x27;ip_address&#x27; =&gt; $this-&gt;input-&gt;ip_address(),<br />
153&nbsp;&nbsp;&nbsp;&nbsp;&#x27;word&#x27; =&gt; $cap[&#x27;word&#x27;]<br />
154&nbsp;&nbsp;&nbsp;&nbsp;);<br />
155<br />
156$query = $this-&gt;db-&gt;insert_string(&#x27;captcha&#x27;, $data);<br />
157$this-&gt;db-&gt;query($query);<br />
158<br />
159echo &#x27;Submit the word you see below:&#x27;;<br />
160echo $cap[&#x27;image&#x27;];<br />
161echo &#x27;&lt;input type=&quot;text&quot; name=&quot;captcha&quot; value=&quot;&quot; /&gt;&#x27;;</code>
162
163 <p>Then, on the page that accepts the submission you'll have something like this:</p>
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Jonesb41032d2010-03-05 11:22:45 -0600165<code>// First, delete old captchas<br />
166$expiration = time()-7200; // Two hour limit<br />
Barry Mienydd671972010-10-04 16:33:58 +0200167$this-&gt;db-&gt;query(&quot;DELETE FROM captcha WHERE captcha_time &lt; &quot;.$expiration); <br />
Derek Jonesb41032d2010-03-05 11:22:45 -0600168<br />
169// Then see if a captcha exists:<br />
Phil Sturgeon8fd08a12011-04-02 12:26:38 +0100170$sql = &quot;SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time &gt; ?&quot;;<br />
Derek Jonesb41032d2010-03-05 11:22:45 -0600171$binds = array($_POST[&#x27;captcha&#x27;], $this-&gt;input-&gt;ip_address(), $expiration);<br />
172$query = $this-&gt;db-&gt;query($sql, $binds);<br />
173$row = $query-&gt;row();<br />
174<br />
175if ($row-&gt;count == 0)<br />
176{<br />
177&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;You must submit the word that appears in the image&quot;;<br />
178}</code>
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Jonesb41032d2010-03-05 11:22:45 -0600180</div>
181<!-- END CONTENT -->
182
183
184<div id="footer">
185<p>
186Previous Topic:&nbsp;&nbsp;<a href="url_helper.html">URL Helper</a>
187&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
188<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
189<a href="../index.html">User Guide Home</a>
190</p>
Derek Jones898949f2011-01-28 07:42:16 -0600191<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2011 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
Derek Jonesb41032d2010-03-05 11:22:45 -0600192</div>
193
194</body>
Greg Akere1f6e9d2010-11-01 12:24:00 -0500195</html>