CAPTCHA stands for “Completely Automated Public Turing test to tell Computers and Humans Apart”, it’s very common functionality to use at the time of submit data to prevent machines access of your website. In this post we will developed CAPTCHA script suing PHP with help of easy three steps.This is very basic captcha which is generated by PHP.
Following files will use in this Captcha Example
catpcha.jpg – A Image for catcha
captcha.php – This file is use to generate captcha with PHP using PHP session.
index.php – This file is use to show captcha image to end user.
Step 1
This image will used to generate captcha image using PHP GD library.This is a only dependency(GD library)
Step 2
We need to create a new file captcha.php
copy the code below in captcha.php
<span class="php"><span class="php-script-tag"><?php</span>
<span class="php-function">session_start</span><span class="php-brackets">(</span><span class="php-brackets">)</span>;
<span class="php-function">header</span><span class="php-brackets">(</span><span class="php-string">"Expires: Mon, 26 Jul 1997 05:00:00 GMT"</span><span class="php-brackets">)</span>;
<span class="php-function">header</span><span class="php-brackets">(</span><span class="php-string">"Last-Modified: "</span> <span class="php-operator">.</span> <span class="php-function">gmdate</span><span class="php-brackets">(</span><span class="php-string">"D, d M Y H:i:s"</span><span class="php-brackets">)</span> <span class="php-operator">.</span> <span class="php-string">" GMT"</span><span class="php-brackets">)</span>;
<span class="php-function">header</span><span class="php-brackets">(</span><span class="php-string">"Cache-Control: no-store, no-cache, must-revalidate"</span><span class="php-brackets">)</span>;
<span class="php-function">header</span><span class="php-brackets">(</span><span class="php-string">"Cache-Control: post-check=0, pre-check=0"</span>, <span class="php-keyword">false</span><span class="php-brackets">)</span>;
<span class="php-function">header</span><span class="php-brackets">(</span><span class="php-string">"Pragma: no-cache"</span><span class="php-brackets">)</span>;
<span class="php-function">function</span> _generateRandom<span class="php-brackets">(</span><span class="php-var">$length</span><span class="php-operator">=</span><span class="php-number">6</span><span class="php-brackets">)</span>
<span class="php-brackets">{</span>
<span class="php-var">$_rand_src</span> <span class="php-operator">=</span> <span class="php-keyword">array</span><span class="php-brackets">(</span>
<span class="php-keyword">array</span><span class="php-brackets">(</span><span class="php-number">4</span><span class="php-number">8</span>,<span class="php-number">5</span><span class="php-number">7</span><span class="php-brackets">)</span> <span class="php-comment">//digits </span>
, <span class="php-keyword">array</span><span class="php-brackets">(</span><span class="php-number">9</span><span class="php-number">7</span>,<span class="php-number">1</span><span class="php-number">2</span><span class="php-number">2</span><span class="php-brackets">)</span> <span class="php-comment">//lowercase chars </span>
<span class="php-comment">// , array(65,90) //uppercase chars </span>
<span class="php-brackets">)</span>;
<span class="php-function">srand</span> <span class="php-brackets">(</span><span class="php-brackets">(</span>double<span class="php-brackets">)</span> <span class="php-function">microtime</span><span class="php-brackets">(</span><span class="php-brackets">)</span> <span class="php-operator">*</span> <span class="php-number">1</span><span class="php-number">0</span><span class="php-number">0</span><span class="php-number">0</span><span class="php-number">0</span><span class="php-number">0</span><span class="php-number">0</span><span class="php-brackets">)</span>;
<span class="php-var">$random_string</span> <span class="php-operator">=</span> <span class="php-string">""</span>;
<span class="php-keyword">for</span><span class="php-brackets">(</span><span class="php-var">$i</span><span class="php-operator">=</span><span class="php-number">0</span>;<span class="php-var">$i</span><span class="php-operator"><</span><span class="php-var">$length</span>;<span class="php-var">$i</span><span class="php-operator">+</span><span class="php-operator">+</span><span class="php-brackets">)</span><span class="php-brackets">{</span>
<span class="php-var">$i1</span><span class="php-operator">=</span><span class="php-function">rand</span><span class="php-brackets">(</span><span class="php-number">0</span>,<span class="php-function">sizeof</span><span class="php-brackets">(</span><span class="php-var">$_rand_src</span><span class="php-brackets">)</span><span class="php-operator">-</span><span class="php-number">1</span><span class="php-brackets">)</span>;
<span class="php-var">$random_string</span> <span class="php-operator">.</span><span class="php-operator">=</span> <span class="php-function">chr</span><span class="php-brackets">(</span><span class="php-function">rand</span><span class="php-brackets">(</span><span class="php-var">$_rand_src</span><span class="php-brackets">[</span><span class="php-var">$i1</span><span class="php-brackets">]</span><span class="php-brackets">[</span><span class="php-number">0</span><span class="php-brackets">]</span>,<span class="php-var">$_rand_src</span><span class="php-brackets">[</span><span class="php-var">$i1</span><span class="php-brackets">]</span><span class="php-brackets">[</span><span class="php-number">1</span><span class="php-brackets">]</span><span class="php-brackets">)</span><span class="php-brackets">)</span>;
<span class="php-brackets">}</span>
<span class="php-keyword">return</span> <span class="php-var">$random_string</span>;
<span class="php-brackets">}</span>
<span class="php-var">$im</span> <span class="php-operator">=</span> @<span class="php-function">imagecreatefromjpeg</span><span class="php-brackets">(</span><span class="php-string">"captcha.jpg"</span><span class="php-brackets">)</span>;
<span class="php-var">$rand</span> <span class="php-operator">=</span> _generateRandom<span class="php-brackets">(</span><span class="php-number">3</span><span class="php-brackets">)</span>;
<span class="php-var">$_SESSION</span><span class="php-brackets">[</span><span class="php-string">'captcha'</span><span class="php-brackets">]</span> <span class="php-operator">=</span> <span class="php-var">$rand</span>;
ImageString<span class="php-brackets">(</span><span class="php-var">$im</span>, <span class="php-number">5</span>, <span class="php-number">2</span>, <span class="php-number">2</span>, <span class="php-var">$rand</span><span class="php-brackets">[</span><span class="php-number">0</span><span class="php-brackets">]</span><span class="php-operator">.</span><span class="php-string">" "</span><span class="php-operator">.</span><span class="php-var">$rand</span><span class="php-brackets">[</span><span class="php-number">1</span><span class="php-brackets">]</span><span class="php-operator">.</span><span class="php-string">" "</span><span class="php-operator">.</span><span class="php-var">$rand</span><span class="php-brackets">[</span><span class="php-number">2</span><span class="php-brackets">]</span><span class="php-operator">.</span><span class="php-string">" "</span>, ImageColorAllocate <span class="php-brackets">(</span><span class="php-var">$im</span>, <span class="php-number">0</span>, <span class="php-number">0</span>, <span class="php-number">0</span><span class="php-brackets">)</span><span class="php-brackets">)</span>;
<span class="php-var">$rand</span> <span class="php-operator">=</span> _generateRandom<span class="php-brackets">(</span><span class="php-number">3</span><span class="php-brackets">)</span>;
ImageString<span class="php-brackets">(</span><span class="php-var">$im</span>, <span class="php-number">5</span>, <span class="php-number">2</span>, <span class="php-number">2</span>, <span class="php-string">" "</span><span class="php-operator">.</span><span class="php-var">$rand</span><span class="php-brackets">[</span><span class="php-number">0</span><span class="php-brackets">]</span><span class="php-operator">.</span><span class="php-string">" "</span><span class="php-operator">.</span><span class="php-var">$rand</span><span class="php-brackets">[</span><span class="php-number">1</span><span class="php-brackets">]</span><span class="php-operator">.</span><span class="php-string">" "</span><span class="php-operator">.</span><span class="php-var">$rand</span><span class="php-brackets">[</span><span class="php-number">2</span><span class="php-brackets">]</span>, ImageColorAllocate <span class="php-brackets">(</span><span class="php-var">$im</span>, <span class="php-number">2</span><span class="php-number">5</span><span class="php-number">5</span>, <span class="php-number">0</span>, <span class="php-number">0</span><span class="php-brackets">)</span><span class="php-brackets">)</span>;
Header <span class="php-brackets">(</span><span class="php-string">'Content-type: image/jpeg'</span><span class="php-brackets">)</span>;
<span class="php-function">imagejpeg</span><span class="php-brackets">(</span><span class="php-var">$im</span>,NULL,<span class="php-number">1</span><span class="php-number">0</span><span class="php-number">0</span><span class="php-brackets">)</span>;
ImageDestroy<span class="php-brackets">(</span><span class="php-var">$im</span><span class="php-brackets">)</span>;
<span class="php-script-tag">?></span></span>
Step 3
We will create template file index.php
copy the code below in index.php
<span class="php"><span class="php-operator"><</span>div <span class="php-keyword">class</span><span class="php-operator">=</span><span class="php-string">"container"</span><span class="php-operator">></span>
<span class="php-operator"><</span>h<span class="php-number">1</span><span class="php-operator">></span>A Simple Example Of PHP CAPTCHA Script<span class="php-operator"><</span><span class="php-operator">/</span>h<span class="php-number">1</span><span class="php-operator">></span>
<span class="php-script-tag"><?php</span>
<span class="php-keyword">if</span><span class="php-brackets">(</span><span class="php-keyword">isset</span><span class="php-brackets">(</span><span class="php-var">$_POST</span><span class="php-brackets">[</span><span class="php-string">"captcha"</span><span class="php-brackets">]</span><span class="php-brackets">)</span><span class="php-brackets">)</span>
<span class="php-keyword">if</span><span class="php-brackets">(</span><span class="php-var">$_SESSION</span><span class="php-brackets">[</span><span class="php-string">"captcha"</span><span class="php-brackets">]</span><span class="php-operator">=</span><span class="php-operator">=</span><span class="php-var">$_POST</span><span class="php-brackets">[</span><span class="php-string">"captcha"</span><span class="php-brackets">]</span><span class="php-brackets">)</span>
<span class="php-brackets">{</span>
<span class="php-comment">//CAPTHCA is valid; proceed the message: save to database, send by e-mail … </span>
<span class="php-keyword">echo</span> <span class="php-string">'<div class="alert alert-success">CAPTHCA is valid; proceed the message</div>'</span>;
<span class="php-brackets">}</span>
<span class="php-keyword">else</span>
<span class="php-brackets">{</span>
<span class="php-keyword">echo</span> <span class="php-string">'<div class="alert alert-danger">CAPTHCA is not valid; ignore submission</div>'</span>;
<span class="php-brackets">}</span>
<span class="php-script-tag">?><span class="html">
<span class="html-form-element"><form role=<span class="html-attribute">"form"</span> method=<span class="html-attribute">"post"</span>></span>
<span class="html-other-element"><div class=<span class="html-attribute">"form-group"</span>></span>
<span class="html-other-element"><label for=<span class="html-attribute">"email"</span>></span>Email address:<span class="html-other-element"></label></span>
<span class="html-form-element"><input type=<span class="html-attribute">"email"</span> class=<span class="html-attribute">"form-control"</span> id=<span class="html-attribute">"email"</span>></span>
<span class="html-other-element"></div></span>
<span class="html-other-element"><div class=<span class="html-attribute">"form-group"</span>></span>
<span class="html-other-element"><label for=<span class="html-attribute">"pwd"</span>></span>Password:<span class="html-other-element"></label></span>
<span class="html-form-element"><input type=<span class="html-attribute">"password"</span> class=<span class="html-attribute">"form-control"</span> id=<span class="html-attribute">"pwd"</span>></span>
<span class="html-other-element"></div></span>
<span class="html-other-element"><div class=<span class="html-attribute">"form-group"</span>></span>
<span class="html-other-element"><div class=<span class="html-attribute">"col-sm-5 pull-left"</span>></span><span class="html-other-element"><label for=<span class="html-attribute">"pwd"</span>></span>Anti Spam code, Please Enter 3 Black Symbols<span class="html-other-element"></label></span>
<span class="html-image-element"><img src=<span class="html-attribute">"captcha.php"</span> alt=<span class="html-attribute">"captcha image"</span>></span><span class="html-other-element"></div></span>
<span class="html-other-element"><div class=<span class="html-attribute">"col-sm-7 pull-right"</span>></span><span class="html-form-element"><input type=<span class="html-attribute">"text"</span> name=<span class="html-attribute">"captcha"</span> size=<span class="html-attribute">"3″ maxlength="</span>3″ class=<span class="html-attribute">"form-control"</span>></span><span class="html-other-element"></div></span>
<span class="html-other-element"></div></span>
<span class="html-other-element"><div class=<span class="html-attribute">"form-group"</span> style=<span class="html-attribute">"<span class="css"><span class="css-property">padding-top<span class="css-selector">:</span><span class="css-value">75px</span></span>;</span>"</span>></span>
<span class="html-form-element"><button type=<span class="html-attribute">"submit"</span> class=<span class="html-attribute">"btn btn-primary"</span>></span>Submit<span class="html-form-element"></button></span>
<span class="html-other-element"></div></span>
<span class="html-form-element"></form></span>
<span class="html-other-element"></div>
</span></span></span></span>