<?php/********************************************************************************* MP3 captchaform - Put this at the top of your form php page********************************************************************************/ /* adjust your path to the mp3captchaform.php location */include_once('./include/mp3captchaform.php'); /* init captchaform class */$mp3captcha= new mp3captcha(); /* set the current url */$mp3captcha->url = $_SERVER['SCRIPT_NAME']; /* language switch */$mp3captcha->langswitch();if (isset($_GET['cfsnd'])) { /* output captcha mp3 */ $mp3captcha->mp3stitch();} else if (isset($_GET['cfimg'])) { /* output captcha image */ $mp3captcha->image();} /* validate captcha value on post - validation can also be done on a different page set the $mp3captcha= new mp3captcha(); first on a different page */if ($mp3captcha->post()) { // Do your POST action here}?><?php/********************************************************************************* MP3 captchaform HTML - Echo this in your form********************************************************************************/echo $mp3captcha->html(); ?>
<?php /* Create a page for the mp3 and link to it in the below javascript Set the : var mp3cf to link to this page as sample: var mp3cf = "/captcha/index.php"; */include_once('./include/mp3captchaform.php'); /* init captchaform class */$mp3captcha = new mp3captcha();$your_captcha_sessionname = "captcha_session_name";$mp3captcha->session = $your_captcha_sessionname; /* create mp3 */if (isset($_GET['cfsnd'])) { $mp3captcha->mp3stitch();}?><?php/* validation part */if (strtolower($_POST['your_post_captcha_form_value']) == strtolower($_SESSION[$your_captcha_sessionname])) { echo "It passed";}/**Javascript and Html to use with your form<script type="text/javascript"><!-- // url of the Mp3Captcha.php ( to webroot ) // Set the : var mp3cf to link to this page, as sample: var mp3cf = "/captcha/index.php"; // click delay 1500 = 1.5 sec var delaytime = 2500; var delayer = false; var desoundEmbed = null; function captchaMp3() { var d = new Date(); if (delayer) { return false; } delayer = true; setTimeout('resetdelay()', delaytime); if (!desoundEmbed) { desoundEmbed = document.createElement("embed"); desoundEmbed.setAttribute("src", mp3cf + "?cfsnd=" + d.getTime()); desoundEmbed.setAttribute("hidden", true); desoundEmbed.setAttribute("autostart", true); desoundEmbed.setAttribute("type", "audio/x-mpeg"); } else { document.body.removeChild(desoundEmbed); desoundEmbed.removed = true; desoundEmbed = null; desoundEmbed = document.createElement("embed"); desoundEmbed.setAttribute("src", mp3cf + "?cfsnd=" + d.getTime()); desoundEmbed.setAttribute("hidden", true); desoundEmbed.setAttribute("autostart", true); desoundEmbed.setAttribute("type", "audio/x-mpeg"); } desoundEmbed.removed = false; document.body.appendChild(desoundEmbed); return true; } function resetdelay() { delayer = false; }//--></script><a href="javascript:captchaMp3();void(0)" onmouseover="window.status=''; return true;">Listen to code</a>*/?>
The generated sound file has a small size ( captcha with 4 characters approx 12 to 25kb ) due to mp3 format with high compression. Captcha fonts, size, length and the background images are adjustable and can be set random or fixed ( Configure as you wish.. ). It's tested on (the latest): Firefox, Opera, Safari, Netscape and Internet Explorer browser ( Win and OSX )