A simple php class to generate and check captcha values.
Notes:
- Single file class, no database needed.
- The check is not case sensitive.
- A token can be used within 1 hour (customizable static property).
$formToken = BasicCaptcha::generateFormToken(); to obtain a new token to be sent alongside the captcha value inserted by the form user.$captchaValue = BasicCaptcha::generate($formToken);, passing the previously obtained form token, to obtain a new captcha value.$captchaBase64 = BasicCaptcha::getB64($captchaValue);, passing the previously obtained captcha value, to obtain a base64 encoded image of the captcha value.<img src="data:image/png;base64,<?php echo $captchaBase64;?>"> node, needed to show the user the captcha image.<input type="text" maxlength="8" name="captcha" placeholder="Input the captcha code" required /> input, needed to let the user input the captcha code.$isCaptchaValid = BasicCaptcha::verify($captcha,$formToken); making sure both the arguments are obtained from the post submission, to check if the captcha input is valid.You can obtain an array of base64 audios by using BasicCaptcha::getB64($captchaValue,'audio','en');
on your page, add all the obtained audios using hidden audio elements: <audio style="display:none;" class="captcha_audio" src="data:audio/mpeg;base64,<?php echo $a; ?>"></audio>
and a button to play them all: <button type="button" id="captcha_audio_button"> Play captcha audio </button>
then include the BasicCaptcha.js class, and use it as follows :
let captcha = new BasicCaptcha({'wrapperQuery':'#captcha-wrapper' ,'logEnabled':true ,'audioPauseDurationMs':800});
captcha.setAudioNodes('.captcha_audio');
captcha.setAudioPlayer('#captcha_audio_button');
You can try a simple form implementation including demo.php, BasicCaptcha.php and BasicCaptcha.js in your project. Don’t forget to remove the file when no more needed.