SimpleCaptcha allows you to drop in an easy-to-use and understand captcha implementation for helping to curb spambots on your system. The plug-in makes things very easy for you with a limited amount of setup required and standard xhtml and css classes in place to make changing the appearance super easy.
There are three pieces to this plug-in: (1) the JavaScript file, (2) the PHP script, and (3) the images. Here are the steps you need to take to get things working:
<form ...>
...
<div id="captcha"></div>
...
</form>
<script>
$('#captcha').simpleCaptcha({
// any options you want to set
});
</script>
When your form is submitted, you just need to check one variable to see if the form was submitted by a human:
<?php
// This is the PHP code you need to use on form submission to see if the user got the
// captcha right. Note that this is for "POST" requests, and you may need to alter the
// code to suit your needs.
if (isset($_SESSION['simpleCaptchaAnswer']) && $_POST['captchaSelection'] == $_SESSION['simpleCaptchaAnswer']) {
// They're human! Continue processing the rest of the form
}
?>
Note that the element you call simpleCaptcha()
on should be in a <form>
!
true
)5
)"captchaText"
)<span id='captchaText'></span>
(default: "<p>To make sure you are a human, we need you to click on the <span class='captchaText'></span>.</p>"
)"<input type='button' value='Refresh Options' />"
)"refreshCaptcha"
)"captchaSelection"
)"simpleCaptcha.php"
)"captchaIntro"
)"captchaImages"
)"captchaImage"
)captcha
(SimpleCaptcha
object))returnData
(object))captcha
(SimpleCaptcha
object))captcha
(SimpleCaptcha
object), hash
(string))captcha
(SimpleCaptcha
object))errorMessage
(string))$('#captcha')
.bind('select.simpleCaptcha', function(hashSelected) {
// do something when the user selects a captcha image
})
.simpleCaptcha({});
What Happens When you call $("#someDivInYourForm").simpleCaptcha({...}) the plug-in will make an XMLHttpRequest (AJAX call) to the PHP script which will then randomly select the number of images you specify (or a default) and from those, one to be the "correct" option. The script places a hash of the image text of the correct option (and some random characters) into the session and sends back the hashes - as well as the file locations - for all options. The hash is placed in the "alt" text of the image tag, and when a user clicks on an image, its hash is placed in the value of the hidden input.
Telling humans from machines When a user submits the form that the simpleCaptcha UI is in, they will also submit the simpleCaptcha hidden form input, you must check this input to see if they got the answer right. All you do is compare their answer (a hash) against what is in the session variable. Check the examples to see how to do this.
Advanced Stuff There are other options and possibilities with this plug-in. For example, the PHP script also places a timestamp of when the captcha was generated into the session ("simpleCaptchaTimestamp"). This allows you to check - when the form is submitted - to see how long the "user" took.
There are also classes that you can specify to allow you to hook into the UI easier, events that are triggered when the captcha is loaded and when a selection is made, and of course the ability to tinker with the location of the script, or even rewrite the script in the language of your choice.
Version | Tag | Published |
---|---|---|
1.0.0 | latest | 7yrs ago |