Salah satu yang mungkin sangat penting dibutuhkan dalam sebuah website yang interaktif adalah adanya kode verifikasi gambar atau yang biasanya disebut dengan CAPTCHA. Plugin captcha sendiri nampaknya sudah cukup banyak beredar, bahkan reCaptcha sendiri telah membuat plugin untuk WordPress. Pertanyaannya, se-simple apakah Captcha Verification Code yang bisa dibuat? Bisakah kita membuat captcha kita sendiri?

PHP Script: Captcha Image Generator

PHP dengan library GD memungkinkan kita membuat gambar captcha dengan algoritma kita sendiri. Berikut adalah fungsi PHP yang bisa digunakan untuk membuat gambar Captcha.

<?php

/* PHP Captcha Image Generator by Arie Putranto
    http://arie.putranto.com/blog/php-script/php-captcha-image-verification.html/ */

session_start();

// Font configuration
$font_size = 16;
$font_file = 'cour.ttf';
$string_length = 5;

// Calculating image dimension based on font configuration
$img_height = $font_size * 2;
$img_width = ($string_length * $font_size) + $font_size;

// Define the code for showing on the image
$_SESSION['validation'] = $string = (isset($_SESSION['code'])) ? substr($_SESSION['code'],0,$string_length) : strtoupper(substr(md5(rand(00000,99999)),0,5));

// Generating the captcha image
$image = @imagecreatetruecolor($img_width, $img_height);

// Define image colors
$bg_color = @imagecolorallocate($image, 252, 252, 252);
$border_color = @imagecolorallocate($image, 200, 200, 200);
$string_color = @imagecolorallocate($image, 100, 100, 100);

// Drawing the image backgroung and borders
@imagefill($image,0,0,$bg_color);
@imageline($image,0,0,0,$img_height,$border_color);
@imageline($image,$img_width-1,0,$img_width-1,$img_height,$border_color);
@imageline($image,0,0,$img_width-1,0,$border_color);
@imageline($image,0,$img_height-1,$img_width-1,$img_height-1,$border_color);

// Write the verification code into the image
for($i=0;$i< $string_length;$i++) {
 @imagettftext($image,$font_size,0,($font_size/2)+2+($font_size*$i),$img_height-($img_height/3)+1,$string_color,$font_file,$string[$i]);
}

// Load the image into the browser
header("Expires: Sun, 1 Jan 2000 12:00:00 GMT");
header("Last-Modified: " . @gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-Type: image/jpeg");

@imagejpeg($image, NULL, 100);
@imagedestroy($image);

?>

Gambar CAPTCHA yang dihasilkan adalah seperti gambar berikut

Menggunakan captcha pada form

Untuk menggunakan captcha tersebut pada form, Anda harus membuat sebuah file PHP yang berisikan kode seperti pada contoh berikut:

<?php

session_start();

// Generating the verification code for the captcha image
$_SESSION['code'] = strtoupper(md5(rand(00000,99999)));

// Processing the form request
if(isset($_POST['captcha'])) {
 $posted_captcha = stripslashes($_POST['captcha']);
 $validation = $_SESSION['validation'];
 if($posted_captcha != $validation) print 'Invalid captcha verification';
 else print 'Captcha code is verified';
} else {
 print 'Insert the number below (<em>Case sensitive</em>)';
}

?>
<img src="./captcha.php" alt="" />

<form name="test" method="post" enctype="multipart/form-data" action="">
<input type="text" name="captcha" value="" />

<input type="submit" value="Submit" />
</form>

Referensi

  1. Demo verifikasi form dengan Captcha
18 comments on "Kode Verifikasi Captcha dengan PHP"
Leave a Response