PHP Classes

File: display_random.php

Recommend this page to a friend!
  Classes of Bogdan Lupandin   CAPTCHA with Audio   display_random.php   Download  
File: display_random.php
Role: Auxiliary script
Content type: text/plain
Description: Random captcha example
Class: CAPTCHA with Audio
Authenticate human users with audio CAPTCHA
Author: By
Last change:
Date: 6 years ago
Size: 2,358 bytes


Class file image Download

$captcha = array('math', 'string');

// Captcha instructions (if any) Later would be displayed accordingly to the captcha displayed
$captcha_inst = array('math' => '',
'string' => 'Do not include spaces. All lower case.');

// The type of captcha we are doing (the key to the $captcha array
$type = rand(0, (count($captcha) - 1));

// The file path of the random captcha
$file_path = "./captcha/{$captcha[$type]}.php";

        <title>String Captcha</title>
        <style type="text/css">
                width: 16px;
                height: 16px;
                border: none;
                cursor: pointer;
                color: #ff0000;
                float: left;
        <div class="inline">
            <img src="<?=$file_path;?>" alt="captcha" />
        <div class="inline">
            <a onclick="document.getElementById('demo').play()" class="captchaLink" >&#9655;</a><br />
            <a onclick="document.getElementById('demo').pause()" class="captchaLink" >&#9015;&#9015;</a>
        <div style="clear: both;"></div>
        <audio id="demo">
            <source src="./audio/<?=$captcha[$type];?>_audio.php?aud=<?php echo time(); ?>" type="audio/mpeg" />
            Your browser does not support the audio element.
        <!-- Displaying the instructions to the captcha (if any) accordingly to the captcha displayed //-->
        <form action="display_random.php" method="POST">
            <input type="text" name="captcha" /><br />
            <input type="submit" name="submit" value="Submit" />
            <input type="reset" value="Reset" />
// Checking the session captcha vs the submitted captcha
if($_SESSION['captcha'] == $_POST['captcha'])
// It was a match
echo 'Correct!';
// It was NOT a match
echo 'Incorrect, correct answer was ' . $_SESSION['captcha'];