Captcha with Figures

index.php
<?phpinclude 'class-captcha.php';

$captcha1 = new mathcaptcha();

$captcha1->generatekode();

?>



<html>

<head>

<title>bloG-ngelantur</title>

</head>

<body>

<h1>Form Entri Data</h1>

<form method="post" action="cek.php">

<table>

<tr><td>Nama Anda</td><td>:</td><td><input type="text" name="nama"></td></tr>

<tr><td>Email Anda</td><td>:</td><td><input type="text" name="email"></td></tr>

</table>   

<p><b>Kode Verifikasi</b></p>

<p>

<? $captcha1->showcaptcha(); ?>

<br>

<input type="text" name="kode">

</p>

<p><input type="submit" name="submit" value="Submit"></p>

</form>

</body>

</html>

cek.php
<?php include 'class-captcha.php';
$captcha1 = new mathcaptcha();

if ($captcha1->resultcaptcha() == $_POST['kode']){
   
    echo "<p><b>Kode Captcha benar.</b></p>";

}els{
   
    echo "<p><b>Kode Captcha salah.</b></p>";
}

echo "<p><a href='index.php'>Back</a></p>"; ?>
class-captcha.php
<?php session_start();

class mathcaptcha
{
    private $bil1;
    private $bil2;
    private $operator;

    function initial()
    {
        $listoperator = array('+', '-', 'x');
        $this->bil1 = rand(0, 99);
        $this->bil2 = rand(0, 99);
        $this->operator = $listoperator[rand(0, 2)];
    }

    function generatekode()
    {
        $this->initial();

        if ($this->operator == '+') $hasil = $this->bil1 + $this->bil2;
        else if ($this->operator == '-') $hasil = $this->bil1 - $this->bil2;
        else if ($this->operator == 'x') $hasil = $this->bil1 * $this->bil2;
        $_SESSION['kode'] = $hasil;
    }

    function showcaptcha()
    {
        echo "Berapa hasil dari ".$this->bil1." ".$this->operator." ".$this->bil2." ? ";
    }   

    function resultcaptcha()
    {
        return $_SESSION['kode'];
    }

} ?> 
Download sample

0 komentar: