Remove file execution permission.
[living-lab-site.git] / user_guide / helpers / captcha_helper.html
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>
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>
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;
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&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
71 <h2>create_captcha(<var>$data</var>)</h2>
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 &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
86         extension.  It will be a number like this:  1139612155.3422</p>
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>
90
91         <h3>Using the CAPTCHA helper</h3>
92
93         <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 />
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
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
119                 is two hours.</li>
120         </ul>
121
122         <h3>Adding a Database</h3>
123
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>
128
129         <p>Here is a table prototype:</p>
130
131 <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 />
138 );</code>
139
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>
141
142 <code>$this-&gt;load-&gt;helper(&#x27;captcha&#x27;);<br />
143 $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 />
159 echo &#x27;Submit the word you see below:&#x27;;<br />
160 echo $cap[&#x27;image&#x27;];<br />
161 echo &#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>
164
165 <code>// First, delete old captchas<br />
166 $expiration = time()-7200; // Two hour limit<br />
167 $this-&gt;db-&gt;query(&quot;DELETE FROM captcha WHERE captcha_time &lt; &quot;.$expiration);   <br />
168 <br />
169 // Then see if a captcha exists:<br />
170 $sql = &quot;SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time &gt; ?&quot;;<br />
171 $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 />
175 if ($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>
179
180 </div>
181 <!-- END CONTENT -->
182
183
184 <div id="footer">
185 <p>
186 Previous 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>
191 <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>
192 </div>
193
194 </body>
195 </html>