
CAPTCHA is used on many website to ensure that the response is not generated by computer. The term “CAPTCHA” was based upon the word capture. It is a contrived acronym for Completely Automated Public Turing test to tell Computers and Humans Apart. A common type of CAPTCHA requires that the user type letters or digits from a distorted image that appears on the screen. (Wikipedia).
Generating Captcha Image Using PHP
PHP allows us to create and/or manipulate image using it’s GD Library. In this article, I will show you how to generate a simple captcha image and use it as a form validation. You can learn more about manipulating image using PHP’s GD Library from the official site.
<?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);
?>
The generated CAPTCHA image will look like this:
Using the captcha image in a form validation
To do that, we need to add a few PHP line on the form and save the form as a PHP file. Check out the code below:
<?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>
Save it as a PHP file. To use the above scripts, simply upload them to the same directory along with the font file. You might need to style the form a bit to match your web design.



12 comments on "PHP Captcha Image Verification"
Tapi ada satu kekurangan dari captcha ini mas. Yaitu agak menyusahkan pengguna yg ingin berkomentar yg menggunakan operamini. Kebanyakan para pengguna opermini mematikan fitur gambarnya, dan tentunya bukan hal yg bijak jika mereka harus merefresh halaman tsb hanya utk mengisi / verifikasi captcha.
Adakah code php lain yg berfungsi sebagai num verification? Soal matematika misalnya…
Bisa saja, bedanya hanya pada generator image saja, untuk verifikasi pada form, tetep serupa
kenapa imagenya ga dikasih “bintik2″ mas? tar ada software khusus yg bs baca kode verifikasinya gmn?
Hello ;) Thank you for this website! Here is mine http://neurontin.wikidot.com/
@benks .. once again it’s just a sample. You can do a lot more such adding dots to the images by editing the script. Thanks
mas knp ga nongol y angka2nya??, padahal script’a sama… mohon bantuanya mas… trimakasih..
Idem dengan Bung Bioz ga muncul euy angka2nya … udah saya cek session bisa berjalan dengan baik maupun saya jalankan di local computer semuanya works well tetapi ketika saya upload ke Server Hosting random number tidak muncul. tq bro Arie
@Bioz + Bayu … hmm .. file untuk font nya sudah diupload juga belum?
kekekeke … belum mas,kmrn setela postink diatas tyt da nemu kalo fontnya kudu diupload dulu.tq bro ..
Bro Arie, jika ingin mengacak tampilan captcha biar tidak terdeteksi spider gimana caranya ya ? aku blm terlalu paham maniuplasi image pake PHP. Bisa kasih conto ? PM aja .. tq bro
I put your code on my website but the image doesn’t appear, could you share with me why the images not appearing.
I’ve any knowledge on PHP.
Hey bro.
I the beginner.
It is assured to you it will be pleasant,new Foto
http://kirstendunst-nude.blogspot.com