Location: PHPKode > projects > BotDetect CAPTCHA > BotDetect-PHP-CAPTCHA-Component-vAlpha1-Build201202051720/WebApp/PhpBasicCaptchaSample/BotDetect/CaptchaHandler.php
<?php 
session_start();

require_once("CaptchaClass.php");

// There are several Captcha commands accessible through the Http interface;
// first we detect which of the valid commands is the current Http request for.
$commandString = StringHelper::Normalize($_GET['get']);
if (!StringHelper::HasValue($commandString)) {
  HttpHelper::BadRequest('command');
}

$command = CaptchaHttpCommand::FromQuerystring($commandString);
switch ($command) {
  case CaptchaHttpCommand::GetImage:
    GetImage();
    break;
  case CaptchaHttpCommand::GetSound:
    GetSound();
    break;
  case CaptchaHttpCommand::GetValidationResult:
    GetValidationResult();
    break;
  default:
    HttpHelper::BadRequest('command');
    break;
}


// Returns the Captcha image binary data
function GetImage() {

  // saved data for the specified Captcha object in the application
  $captcha = GetCaptchaObject();
  if (is_null($captcha)) {
    HttpHelper::BadRequest('captcha');
  }
  
  // identifier of the particular Captcha object instance 
  $instanceId = GetInstanceId();
  if (is_null($instanceId)) {
    HttpHelper::BadRequest('instance');
  }
  
  // response MIME type & headers
  while (ob_get_length()) {
    ob_end_clean();
  }
  ob_start();
  try {
    $mimeType = $captcha->ImageMimeType;
    header("Content-Type: ${mimeType}");
    // image generation
    $rawImage = $captcha->GetImage($instanceId);
    $captcha->Save();
    echo $rawImage;
  } catch (Exception $e) {
    header('Content-Type: text/plain');
    echo $e->getMessage();
  }
  
  ob_end_flush();
  exit;
}


// Returns the Captcha sound binary data
function GetSound() {
  // saved data for the specified Captcha object in the application
  $captcha = GetCaptchaObject();
  if (is_null($captcha)) {
    HttpHelper::BadRequest('captcha');
  }
  
  // identifier of the particular Captcha object instance 
  $instanceId = GetInstanceId();
  if (is_null($instanceId)) {
    HttpHelper::BadRequest('instance');
  }
  
  // response MIME type & headers
  while (ob_get_length()) {
    ob_end_clean();
  }
  ob_start();
  try {
    $mimeType = $captcha->SoundMimeType;
    header("Content-Type: ${mimeType}");
    // sound generation
    $rawSound = $captcha->GetSound($instanceId);
    echo $rawSound;
  } catch (Exception $e) {
    header('Content-Type: text/plain');
    echo $e->getMessage();
  }
  
  ob_end_flush();
  exit;
}


// Used for client-side validation, returns Captcha validation result as JSON
function GetValidationResult() {
  // TODO: implement
}


// gets Captcha instance according to the CaptchaId passed in querystring
function GetCaptchaObject() {
  $captchaId = StringHelper::Normalize($_GET['c']);
  if (!StringHelper::HasValue($captchaId) ||
      !CaptchaBase::IsValidCaptchaId($captchaId)) {
    return;
  }
  $captchaBase = new CaptchaBase($captchaId);
  $captchaBase->Load();
  return $captchaBase; 
}


// extract the exact Captcha code instance referenced by the request
function GetInstanceId() {
  $instanceId = StringHelper::Normalize($_GET['t']);
  if (!StringHelper::HasValue($instanceId) ||
      !CaptchaBase::IsValidInstanceId($instanceId)) {
    return;
  }
  return $instanceId;
}

?>
Return current item: BotDetect CAPTCHA