Form Mail dengan Captcha

 
Tutorial skrip form mail ini adalah lanjutan dari artikel terkait sebelumnya tentang skrip form mail. Kali ini kita beri pengamanan terhadap form mail agar tidak mudah diserang oleh spammer/bot, yakni menggunakan Captcha, sebuah cara mengamankan form mail yang banyak digunakan saat ini.

Dalam contoh artikel ini menggunakan 4 buah file skrip dan 1 file font .TTF. Berikut masing-masing listing skripnya.

File contact_us.html

<html>
<head>
<title>Contact Us</title>
</head>
<body>
<form id=”form1″ name=”form1″ method=”post” action=”action.php”><fieldset><legend align=”center”>Contact Us</legend>
<table width=”75%” border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td width=”13%”>Nama</td>
<td width=”3%”> </td>
<td width=”84%”><input name=”nama” type=”text” id=”nama” /></td>
</tr>
<tr>
<td>E-mail</td>
<td> </td>
<td><input name=”email” type=”text” id=”email” /></td>
</tr>
<tr>
<td>Website</td>
<td> </td>
<td><input name=”site” type=”text” id=”site” /></td>
</tr>
<tr>
<td>Telp.</td>
<td> </td>
<td><input name=”phone” type=”text” id=”phone” /></td>
</tr>
<tr>
<td align=”left” valign=”top”>Komentar</td>
<td> </td>
<td align=”left” valign=”top”><textarea name=”comment” cols=”40″ rows=”5″ id=”comment”></textarea></td>
</tr>
<tr>
<td></td>
<td></td>
<td><img src=”captchasecurityimages.php?width=150&height=40&character=5″ />
Security Code: <input id=”security_code” name=”security_code” type=”text” />
</td>
<tr>
<td align=”left” valign=”top”> </td>
<td> </td>
<td align=”left” valign=”top”><input type=”submit” name=”Submit” value=”Submit” />
<input type=”reset” name=”Submit2″ value=”Reset” /></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>

File action.php

<?
session_start();
if( isset($_POST[‘Submit’])) {
if(($_SESSION[‘security_code’] == $_POST[‘security_code’]) && (!empty($_SESSION[‘security_code’])) ) {
// masukkan script anda jika validasi benar
//Nilai dari Form
$nama=$_REQUEST[‘nama’];
$email=$_REQUEST[’email’];
$website=$_REQUEST[‘site’];
$telp=$_REQUEST[‘phone’];
$komentar=$_REQUEST[‘comment’];
//Deklarasi Variable
$headers = “From: webmaster@namadomain.com Alamat e-mail ini dilindungi dari spambot, anda harus memampukan JavaScript untuk melihatnya “;
$recipient = ” info@namadomain.com Alamat e-mail ini dilindungi dari spambot, anda harus memampukan JavaScript untuk melihatnya “;
$subject = “Contact Us Feedback”;
$message = “Nama : $nama rn
E-mail : $email rn
Website : $website rn
Telephone Number : $telp rn
COMMENTS : $komentar”;
//mail() fungsi untuk mengirim e-mail
mail($recipient , $subject ,$message ,$headers);
//redirect ke halaman terima kasih saat selesai mengirim e-mail
header(“Location: thanks.html”);
} else {
// masukkan script anda jika validasi salah
echo ‘Sorry, anda memasukkan security code yang salah<br />’;
include “contact_us.html”;
}
} else {
include “contact_us.html”;
}
?>

File captchasecurityimages.php

<?php
session_start();
class CaptchaSecurityImages {
var $font = ‘/s/namadomain.com/www/anorexia.ttf’;
function generateCode($characters) {
/* list all possible characters, similar looking characters and vowels have been removed */
$possible = ‘23456789bcdfghjkmnpqrstvwxyz’;
$code = ”;
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}
function CaptchaSecurityImages($width=’120′,$height=’40’,$characters=’6′) {
$code = $this->generateCode($characters);
/* font size will be 75% of the image height */
$font_size = $height * 0.65;
$image = @imagecreate($width, $height) or die(‘Cannot Initialize new GD image stream’);
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 130, 120, 180);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code);
$x = ($width – $textbox[4])/2;
$y = ($height – $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code);
/* output captcha image to browser */
imagejpeg($image);
imagedestroy($image);
$_SESSION[‘security_code’] = $code;
}
}
$width = isset($_GET[‘width’]) ? $_GET[‘width’] : ‘120’;
$height = isset($_GET[‘height’]) ? $_GET[‘height’] : ’40’;
$characters = isset($_GET[‘characters’]) ? $_GET[‘characters’] : ‘6’;
header(‘Content-Type: image/jpeg’);
$captcha = new captchasecurityimages($width,$height,$characters);
?>

File thanks.html

<html>
<head>
<meta http-equiv=”Content-Language” content=”en-us”>
<meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″>
<title>Thank’s</title>
</head>
<body topmargin=”0″ leftmargin=”0″><fieldset><legend align=”center”>Terima Kasih</legend>
<p align=”center”>Terima kasih atas saran dan komentar dari Anda.</p>
</fieldset>
</body>
</html>

NB:

  • Captcha membutuhkan GD library pada php, diset di php.ini
  • letakkan file font dalam satu direktori dengan skrip, silahkan ambil salah satu font TTF dari komputer Anda
  • penulisan path font pada variable $font mesti ditulis absolute.

 

By. Dessy Imawardhani

This entry was posted in Article.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s