<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web Development Blog &#187; recpatcha</title>
	<atom:link href="http://www.web-development-blog.com/archives/tag/recpatcha/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.web-development-blog.com</link>
	<description>Web development tutorials, SEO articles and PHP script resources</description>
	<lastBuildDate>Wed, 25 Aug 2010 19:46:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Create custom reCAPTCHA images using their API</title>
		<link>http://www.web-development-blog.com/archives/create-custom-recaptcha-images-using-their-api/</link>
		<comments>http://www.web-development-blog.com/archives/create-custom-recaptcha-images-using-their-api/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 08:12:28 +0000</pubDate>
		<dc:creator>Olaf</dc:creator>
				<category><![CDATA[PHP scripts]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[jQuery Code]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[form plugin]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[recpatcha]]></category>
		<category><![CDATA[theme]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.web-development-blog.com/?p=715</guid>
		<description><![CDATA[If you need to spam protect your contact, comment or any other public form you should think about using a CAPTCHA image. A CAPTCHA image is an image with a random text which spam bots can&#8217;t read. The visitor need to enter that text into a form field and only if there is a match [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to spam protect your contact, comment or any other public form you should think about using a CAPTCHA image. A CAPTCHA image is an image with a random text which spam bots can&#8217;t read. The visitor need to enter that text into a form field and only if there is a match the form get processed.</p>
<p>Try a search for &#8220;CAPTCHA image&#8221; on Google and you will find a lot of scripts, some of them are powerful and some of them are not very effective. I&#8217;m using <a href="http://recaptcha.net/" rel="nofollow">reCAPTCHA</a> since several years, their challenge image is very effective and safe and if people can&#8217;t read it they can listen to an audio file.</p>
<p><img src="http://www.web-development-blog.com/wp-content/uploads/2009/12/recpatcha_red.png" alt="recpatcha_red" title="recpatcha_red" width="314" height="124" class="aligncenter size-full wp-image-718" /></p>
<p>I&#8217;m sure you have seen this image before, this is the challenge image you get with their default configuration. if you don&#8217;t like this huge RED image (like me) you need a custom theme. The good news is they offer a very powerful <a href="http://recaptcha.net/apidocs/captcha/" rel="nofollow">API system</a> for the client side and also for the server side. In this tutorial we use some jQuery code together with the reCAPTCHA ajax library to generate the challenge images. <span id="more-715"></span>The validation process is done by a PHP script that use a simple cURL function to submit the request and response to the reCAPTCHA server to test the submitted match. We use also the <a href="http://www.malsup.com/jquery/form/" rel="nofollow">jQuery form plugin</a> to process the form fields and client side validation.</p>
<p>If you like to see how it looks like check this <a href="http://www.finalwebsites.com/demos/custom-captcha-image-script/">custom CAPTCHA theme</a> demo page.</p>
<h3>reCAPTCHA client side script</h3>
<p>Before we start we need to register a domain name at the reCAPTCHA website to receive a public and private key. To create a custom challenge image the Ajax way, we need to include the external file <strong>recaptcha_ajax.js</strong> and we use the following JavaScript code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> RecaptchaOptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	theme <span style="color: #339933;">:</span> <span style="color: #3366CC;">'custom'</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Recaptcha.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'your public key'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'recaptcha_image'</span><span style="color: #339933;">,</span> RecaptchaOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Recaptcha.<span style="color: #660066;">focus_response_field</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There is only one option required for this custom function,  check <a href="http://recaptcha.net/apidocs/captcha/client.html#customization" rel="nofollow">this link</a> if you like to set more options.<br />
Next we need a small HTML snippet to show the challenge image:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;recaptcha_image&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;recaptcha_response_field&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;recaptcha_response_field&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Submit&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit_button&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span> 
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span></pre></div></div>

<p>To get this working you need to create a form text field with the name and ID <strong>recaptcha_response_field</strong>, use the same ID name of the target DIV container as you used in the JavaScript function. Using both snippets in used in a HTML page will generate a neutral CAPTCHA image like:</p>
<p><img src="http://www.web-development-blog.com/wp-content/uploads/2009/12/captcha.jpg" alt="captcha" title="captcha" width="300" height="57" class="aligncenter size-full wp-image-720" /></p>
<p>The current code doesn&#8217;t process our CAPTCHA validation script. We do this using <a href="http://docs.jquery.com/Downloading_jQuery" rel="nofollow">jQuery</a> and the jQuery form plugin. In this tutorial we do not explain how the plugin works, we did that already in this <a href="http://www.web-development-blog.com/archives/jquery-form-plugin-and-php-file-uploads/">jQuery form plugin tutorial</a>. In the next JavaScript snippet we check if the text field is not empty and if there is a response we hide the form and show the response in the DIV container <strong>output</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
	<span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> 
        target<span style="color: #339933;">:</span>        <span style="color: #3366CC;">'#output'</span><span style="color: #339933;">,</span>   <span style="color: #006600; font-style: italic;">// target element(s) to be updated with server response </span>
        beforeSubmit<span style="color: #339933;">:</span>  showRequest<span style="color: #339933;">,</span>  <span style="color: #006600; font-style: italic;">// pre-submit callback </span>
        success<span style="color: #339933;">:</span>       showResponse<span style="color: #339933;">,</span>  <span style="color: #006600; font-style: italic;">// post-submit callback </span>
        url<span style="color: #339933;">:</span>       <span style="color: #3366CC;">'recaptcha.php'</span>  <span style="color: #006600; font-style: italic;">// override for form's 'action' attribute </span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> 
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myform'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
        $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ajaxSubmit</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> RecaptchaOptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		theme <span style="color: #339933;">:</span> <span style="color: #3366CC;">'custom'</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
 	Recaptcha.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'your public key'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'recaptcha_image'</span><span style="color: #339933;">,</span> RecaptchaOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #006600; font-style: italic;">// pre-submit callback </span>
<span style="color: #003366; font-weight: bold;">function</span> showRequest<span style="color: #009900;">&#40;</span>formData<span style="color: #339933;">,</span> jqForm<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
	<span style="color: #003366; font-weight: bold;">var</span> resp_field <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[name=recaptcha_response_field]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fieldValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>resp_field<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'You need to enter the validation string.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span> 
<span style="color: #006600; font-style: italic;">// post-submit callback </span>
<span style="color: #003366; font-weight: bold;">function</span> showResponse<span style="color: #009900;">&#40;</span>responseText<span style="color: #339933;">,</span> statusText<span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span> 
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>statusText <span style="color: #339933;">==</span> <span style="color: #3366CC;">'success'</span> <span style="color: #339933;">&amp;&amp;</span> responseText <span style="color: #339933;">!=</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#formcont'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Error while processing your request<span style="color: #000099; font-weight: bold;">\n</span>Please try it again on a later moment.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Server side validation script</h3>
<p>Next we need to create a PHP script called <strong>recaptcha.php</strong> that will validate the entered text from our CAPTCHA image.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'recaptcha_response_field'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$answer</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'recaptcha_response_field'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$postData</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'challenge'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'recaptcha_challenge_field'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>	
	<span style="color: #000088;">$postData</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'response'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$answer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$postData</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'remoteip'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$postData</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'privatekey'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'your private key'</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://api-verify.recaptcha.net/verify&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #000088;">$postData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span>PHP_EOL<span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'true'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Success, you may proceed!'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Error, please try again.'</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// check the API documentation for detailed error reports</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this script we create a post array for the cURL function and post all the data to the reCAPTCHA API URL. Note all used variables are required, the post variable <strong>recaptcha_challenge_field</strong> is generated by the client API. The value for the challenge entry need to be encoded before we can post that value to the reCAPTCHA API. After we received the response we split the string and check if the first part is equal to true and output a success or error message which is presented in the target DIV container.</p>
<p>If you like to use this script on your own website feel free to download the script on our <a href="http://www.finalwebsites.com/forums/forum/php-class-downloads">PHP script forum</a>.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.web-development-blog.com/archives/jquery-contact-form-for-your-website/" rel="bookmark" title="January 11, 2010">jQuery Contact form for your website</a></li>
<li><a href="http://www.web-development-blog.com/archives/ajax-requests-using-jquery-and-php/" rel="bookmark" title="December 24, 2009">Ajax requests using jQuery and PHP</a></li>
<li><a href="http://www.web-development-blog.com/archives/jquery-form-plugin-and-php-file-uploads/" rel="bookmark" title="November 2, 2009">jQuery form plugin and PHP file uploads</a></li>
</ul>
<p><!-- Similar Posts took 3.675 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.web-development-blog.com/archives/create-custom-recaptcha-images-using-their-api/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->