Implementing Audio CAPTCHA
by David Summer


Listing One

<?php
session_start();

// the 4 random numbers 
for($i = 0; $i < 4; $i++)
    $fileName[] = GetRand();
// Get a random number from 0-9 
function GetRand()
{
    return rand(1,10)-1;
}
$_SESSION['captchaAnswer'] = $fileName[0] . $fileName[1] . $fileName[2] . $fileName[3];
?>
<HTML>

<HEAD>
<title>Audio CAPTCHA Test</title>
</HEAD>
<BODY bgcolor="lightblue" onload="Init()">

<SCRIPT language="JavaScript">

// Init the window, by clearing any input from a former try
function Init()
{
    document.getElementById('userAnswer').value = "";
}
var onWindows = true;
var onExplorer = true;
if(navigator.platform.indexOf("Win") == -1)
{
    onWindows = false;
}
if(navigator.appName != "Microsoft Internet Explorer")
{
    onExplorer = false;
}
// grab user key press
document.onkeyup = KeyCheck;       
function KeyCheck(e)
{
    /* for browser independence, IE uses window.event, Firefox passes event */
    var keyPressed = (window.event) ? event.keyCode : e.keyCode;
    var hotKey = 80; // p key
    if(keyPressed == hotKey)
    {
        document.getElementById('userAnswer').value = "";
        /* embed for IE, Media player won't show
        iframe for FireFox, embed doesn't play dynamic file */
        if(onWindows && onExplorer)
        {
            document.getElementById('mediaPlayer').controls.play();
        }
        else
        {
            document.getElementById('writeHere').innerHTML = '<iframe src="./PlaySound.php" width="0" height="0"></iframe>';
        }
        document.getElementById('userAnswer').focus();
    }   
 }
/* if using IE on Windows, use an embedded Windows Media Player */
if(onWindows && onExplorer)
{
    document.write('<OBJECT id="mediaPlayer" \
    CLASSID="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" \
    TYPE="application/x-oleobject" width="0" height="0"> \
    <param name="url" value="./PlaySound.php" /> \
    <param name="autoStart" value="false" /> \
    </OBJECT>');
}
</SCRIPT>
<br>
<!-- the form for instructions and user entry -->
<form method="POST" action="CaptchaSubmit.php">
Press the P key to play the sound file. 
<br>Then type the 4 numbers you hear and press Enter.
<br><input type="text" id="userAnswer" name="userAnswer" maxlength="4" size="4"/>
</form>

<!-- a place to write the frame informaion when not on IE/Windows-->
<div id="writeHere" style="visibility:hidden"></div>
</body>
</html>

Listing Two

<?php 
session_start();
header('Content-type: audio/x-mpeg');
/* put an intro in first */
$thisFile = "Intro.mp3";
$fh = fopen($thisFile, 'r');
$allAudio = fread($fh, filesize($thisFile));
fclose($fh);

/* add the numbers to the file */
$str = $_SESSION['captchaAnswer'];
/* this works under PHP4,
can use str_split() here instead in PHP5 */
$fileName = array();
for ($i = 0; $i < strlen($str); $i++) 
    $fileName[] = substr($str,$i,1);
foreach ($fileName as $fileNumer)
{
    $thisFile = $fileNumer . ".mp3";
    $fh = fopen($thisFile, 'r');
    $allAudio .= fread($fh, filesize($thisFile));
    fclose($fh);
}
/* output the resulting MP3 */
print $allAudio;
?>

Listing Three

<?php

session_start();

header('Content-type: text/html');

// true if correct answer
function CheckAnswer()
{
  if (!isset($_POST['userAnswer']) || !isset($_SESSION['captchaAnswer']))
    return 0;
  else if ($_POST['userAnswer'] == $_SESSION['captchaAnswer']) 
    return 1;
  return 0;  
}
print "<html><head></head><body>";
print $_POST['userAnswer'];
if(CheckAnswer())
    print " is correct.";
else
    print " is not correct, please go back and try again.";
print"</body><html>";
?>


1


