doditsuprianto
11-03-2008, 03:58 PM
CAPTCHA merupakan program untuk melindungi website dari serangan spam/bot yang dilakukan secara otomatis dengan cara melakukan tes apakah manusia dapat melalui kode program pada saat itu. Sebagai contoh, manusia seharusnya masih dapat membaca teks seperti yang tertampak di bawah, diamana komputer tidak dapat melakukannya (karena komputer tidak dapat membedakan antara teks dan gambar pada suatu image yang telah menyatu)
Istilah CAPTCHA kependekan dari Completely Automated Turing Test To Tell Computers and Humans Apart yang mulai dikenal sekitar tahun 2000 oleh Luis von Ahn, Manuel Blum, Nicholas Hopper dan John Langford dari Carnegie Mellon University. Pada saat itu CAPTCHA dikembangkan pertamakali untuk digunakan oleh Yahoo.
Kesempatan kali ini saya membuat program CAPTCHA dengan menggunakan class PHP. Anda dapat menggunakannya dengan mudah program tersebut, yang selanjutnya bisa diterapkan di websites Anda, terutama saat melakukan submit FORM.
Script utama penggunaan dari class captcha ini adalah:
<img src=captcha.class.php?length=&font=&size=&angel=&file=>
Length menunjukan berapa jumlah karakter yang akan ditampilkan. Jika tidak diisi maka secara default akan bernilai 4.
Font adalah jenis font karakter yang akan ditampilkan. Secara default, font yang digunakan adalah georgia.ttf. Jika ingin menggantinya, maka Anda harus menambahkan font baru di folder bersangkutan (misalnya font Arial.ttf), dan parameter font harus diisi menjadi <img src=captcha.class.php?length=&font=Arial.ttf&size=&angel=&file=>
Size menunjukan berapa ukuran dari font yang akan ditampilkan. Jika tidak diisi maka secara default akan bernilai 20. Misal: <img src=captcha.class.php?length=&font=25&size=&angel=&file=>
Angel menunjukkan berapa derajat kemiringan dari teks yang akan ditampilkan. Jika tidak diisi maka secara default bernilai 0 yang berarti teks ditampilkan secara horizontal. Jika bernilai lebih dari 0 maka teks akan miring berlawanan jarum jam sebesar derajat yang dicantumkan. Jika angel bernilai negatif, maka teks akan ditampilkan miring sebesar derajat yang ditentukan searah jarum jam. Misal: <img src=captcha.class.php?length=&font=&size=&angel=5&file=> atau <img src=captcha.class.php?length=&font=&size=&angel=-5&file=>
File adalah nama file gambar bakground. Secara default gambar background bernama “eyes.gif”. Jika diinginkan background lain, maka Anda harus menambah gambarnya sendiri. Kemudian mengisi nama file di parameter yang disediakan. Misalnya: <img src=captcha.class.php?length=5&font=&size=24&angel=5&file=”flower.jpg”>
Penjelasan lengkap ada di http://doditsuprianto.com
Download Custom CAPTCHA (http://doditsuprianto.com/captcha/captcha.zip)
<?php
/**
* @author Dodit Suprianto
* Email: d0dit@yahoo.com
* Website: http://doditsuprianto.com, http://goiklan.co.nr
* Website: http://www.meozit.com, http://easyads.co.nr
*
* call captcha code <img src=captcha.class.php?length=&font=&size=&angel=&file=>
*
* You can enter the parameter or not. Explaination:
*
* length : how much character will be shown. 4 character by default.
* font : Type of font (ex. Arial.ttf). Georgia font by default
* size : Size of font character. 20 by default
* angel : angel of font character. 0 by default, it means horizontal position
* file : if you want subtitute the background image. "eyes.gif" by default.
*
* You must copy several file to current directory
* if you want to subtitute the default setting.
* such as font file, background file
*
*/
session_start();
class captcha
{
private $length;
private $font;
private $size;
private $angel;
private $file;
public function __construct()
{
$this->length = 4;
$this->font = "georgia.ttf";
$this->size = 20;
$this->angel = 0;
$this->file = "eyes.gif";
}
public function setFile($file)
{
$this->file = $file;
}
public function setLength($length)
{
$this->length = $length;
}
public function setFont($font)
{
$this->font = $font;
}
public function setSize($size)
{
$this->size = $size;
}
public function setAngel($angel)
{
$this->angel = $angel;
}
private function RandomText()
{
$output = "";
$input = array('A','B','C','D','E','F','G','H','I','J','K', 'L','M','N','O','P','Q','R','S','T','U','V','W','X ','Y','Z','1','2','3','4','5','6','7','8','9');
srand((float) microtime() * 10000000);
$rand_keys = array_rand($input, count($input));
for ($i=0; $i < count($input)-1; $i++)
{
$output .= $input[$rand_keys[$i]];
}
return substr($output, 0, $this->length);
}
public function ConvertFontToImage()
{
$type = getimagesize($this->file);
$random = $this->RandomText();
switch ($type[2])
{
case IMAGETYPE_GIF:
header("Content-type: image/gif");
$im = imagecreatefromgif($this->file);
$white = imagecolorallocate($im, 255, 255, 255);
$textbox = imagettfbbox($this->size, 0, $this->font, $this->RandomText());
$px = ($type[0] - $textbox[4])/2;
$py = ($type[1] - $textbox[5])/2;
imagettftext($im, $this->size, $this->angel, $px, $py, $white, $this->font, $random);
imagegif($im);
imagedestroy($im);
break;
case IMAGETYPE_JPEG:
header("Content-type: image/jpeg");
$im = imagecreatefromjpeg($this->file);
$white = imagecolorallocate($im, 255, 255, 255);
$textbox = imagettfbbox($this->size, 0, $this->font, $this->RandomText());
$px = ($type[0] - $textbox[4])/2;
$py = ($type[1] - $textbox[5])/2;
imagettftext($im, $this->size, $this->angel, $px, $py, $white, $this->font, $random);
imagejpeg($im);
imagedestroy($im);
break;
case IMAGETYPE_PNG:
header("Content-type: image/png");
$im = imagecreatefrompng($this->file);
$white = imagecolorallocate($im, 255, 255, 255);
$textbox = imagettfbbox($this->size, 0, $this->font, $this->RandomText());
$px = ($type[0] - $textbox[4])/2;
$py = ($type[1] - $textbox[5])/2;
imagettftext($im, $this->size, $this->angel, $px, $py, $white, $this->font, $random);
imagepng($im);
imagedestroy($im);
break;
}
$_SESSION['code'] = $random;
}
}
$handle = new captcha();
if (!empty($_GET['length'])) $handle->setLength($_GET['length']);
if (!empty($_GET['font'])) $handle->setFont($_GET['font']);
if (!empty($_GET['size'])) $handle->setSize($_GET['size']);
if (!empty($_GET['angel'])) $handle->setAngel($_GET['angel']);
if (!empty($_GET['file'])) $handle->setFile($_GET['file']);
$handle->ConvertFontToImage();
?>
<?php
/**
* @author Dodit Suprianto
* Email: d0dit@yahoo.com
* Website: http://doditsuprianto.com, http://goiklan.co.nr
* Website: http://www.meozit.com, http://easyads.co.nr
*
* call captcha code <img src=captcha.class.php?length=&font=&size=&angel=&file=>
*
* You can enter the parameter or not. Explaination:
*
* length : how much character will be shown. 4 character by default.
* font : Type of font (ex. Arial.ttf). Georgia font by default
* size : Size of font character. 20 by default
* angel : angel of font character. 0 by default, it means horizontal position
* file : if you want subtitute the background image. "eyes.gif" by default.
*
* You must copy several file to current directory
* if you want to subtitute the default setting.
* such as font file, background file
*
*/
session_start();
?>
<html>
<head>
<title>Demo Captcha</title>
</head>
<body>
<?php
if ($_POST['submit'])
{
if ($_SESSION['code'] == $_POST['code'] && !empty($_SESSION['code']))
{
/**
* you should add other codes in here
* in this section, means that the captcha and code are match
*/
echo "captcha is correct";
unset($_SESSION['code']);
}
else
{
echo "captcha is wrong";
}
}
echo "<form action='$_SERVER[SELF]' method=POST>";
echo "Security code: <img src=captcha.class.php?length=4&font=&size=24&angel=5&file=><p>";
echo "Input code: <input type=text name=code><br>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form>";
?>
</body>
</html>
Istilah CAPTCHA kependekan dari Completely Automated Turing Test To Tell Computers and Humans Apart yang mulai dikenal sekitar tahun 2000 oleh Luis von Ahn, Manuel Blum, Nicholas Hopper dan John Langford dari Carnegie Mellon University. Pada saat itu CAPTCHA dikembangkan pertamakali untuk digunakan oleh Yahoo.
Kesempatan kali ini saya membuat program CAPTCHA dengan menggunakan class PHP. Anda dapat menggunakannya dengan mudah program tersebut, yang selanjutnya bisa diterapkan di websites Anda, terutama saat melakukan submit FORM.
Script utama penggunaan dari class captcha ini adalah:
<img src=captcha.class.php?length=&font=&size=&angel=&file=>
Length menunjukan berapa jumlah karakter yang akan ditampilkan. Jika tidak diisi maka secara default akan bernilai 4.
Font adalah jenis font karakter yang akan ditampilkan. Secara default, font yang digunakan adalah georgia.ttf. Jika ingin menggantinya, maka Anda harus menambahkan font baru di folder bersangkutan (misalnya font Arial.ttf), dan parameter font harus diisi menjadi <img src=captcha.class.php?length=&font=Arial.ttf&size=&angel=&file=>
Size menunjukan berapa ukuran dari font yang akan ditampilkan. Jika tidak diisi maka secara default akan bernilai 20. Misal: <img src=captcha.class.php?length=&font=25&size=&angel=&file=>
Angel menunjukkan berapa derajat kemiringan dari teks yang akan ditampilkan. Jika tidak diisi maka secara default bernilai 0 yang berarti teks ditampilkan secara horizontal. Jika bernilai lebih dari 0 maka teks akan miring berlawanan jarum jam sebesar derajat yang dicantumkan. Jika angel bernilai negatif, maka teks akan ditampilkan miring sebesar derajat yang ditentukan searah jarum jam. Misal: <img src=captcha.class.php?length=&font=&size=&angel=5&file=> atau <img src=captcha.class.php?length=&font=&size=&angel=-5&file=>
File adalah nama file gambar bakground. Secara default gambar background bernama “eyes.gif”. Jika diinginkan background lain, maka Anda harus menambah gambarnya sendiri. Kemudian mengisi nama file di parameter yang disediakan. Misalnya: <img src=captcha.class.php?length=5&font=&size=24&angel=5&file=”flower.jpg”>
Penjelasan lengkap ada di http://doditsuprianto.com
Download Custom CAPTCHA (http://doditsuprianto.com/captcha/captcha.zip)
<?php
/**
* @author Dodit Suprianto
* Email: d0dit@yahoo.com
* Website: http://doditsuprianto.com, http://goiklan.co.nr
* Website: http://www.meozit.com, http://easyads.co.nr
*
* call captcha code <img src=captcha.class.php?length=&font=&size=&angel=&file=>
*
* You can enter the parameter or not. Explaination:
*
* length : how much character will be shown. 4 character by default.
* font : Type of font (ex. Arial.ttf). Georgia font by default
* size : Size of font character. 20 by default
* angel : angel of font character. 0 by default, it means horizontal position
* file : if you want subtitute the background image. "eyes.gif" by default.
*
* You must copy several file to current directory
* if you want to subtitute the default setting.
* such as font file, background file
*
*/
session_start();
class captcha
{
private $length;
private $font;
private $size;
private $angel;
private $file;
public function __construct()
{
$this->length = 4;
$this->font = "georgia.ttf";
$this->size = 20;
$this->angel = 0;
$this->file = "eyes.gif";
}
public function setFile($file)
{
$this->file = $file;
}
public function setLength($length)
{
$this->length = $length;
}
public function setFont($font)
{
$this->font = $font;
}
public function setSize($size)
{
$this->size = $size;
}
public function setAngel($angel)
{
$this->angel = $angel;
}
private function RandomText()
{
$output = "";
$input = array('A','B','C','D','E','F','G','H','I','J','K', 'L','M','N','O','P','Q','R','S','T','U','V','W','X ','Y','Z','1','2','3','4','5','6','7','8','9');
srand((float) microtime() * 10000000);
$rand_keys = array_rand($input, count($input));
for ($i=0; $i < count($input)-1; $i++)
{
$output .= $input[$rand_keys[$i]];
}
return substr($output, 0, $this->length);
}
public function ConvertFontToImage()
{
$type = getimagesize($this->file);
$random = $this->RandomText();
switch ($type[2])
{
case IMAGETYPE_GIF:
header("Content-type: image/gif");
$im = imagecreatefromgif($this->file);
$white = imagecolorallocate($im, 255, 255, 255);
$textbox = imagettfbbox($this->size, 0, $this->font, $this->RandomText());
$px = ($type[0] - $textbox[4])/2;
$py = ($type[1] - $textbox[5])/2;
imagettftext($im, $this->size, $this->angel, $px, $py, $white, $this->font, $random);
imagegif($im);
imagedestroy($im);
break;
case IMAGETYPE_JPEG:
header("Content-type: image/jpeg");
$im = imagecreatefromjpeg($this->file);
$white = imagecolorallocate($im, 255, 255, 255);
$textbox = imagettfbbox($this->size, 0, $this->font, $this->RandomText());
$px = ($type[0] - $textbox[4])/2;
$py = ($type[1] - $textbox[5])/2;
imagettftext($im, $this->size, $this->angel, $px, $py, $white, $this->font, $random);
imagejpeg($im);
imagedestroy($im);
break;
case IMAGETYPE_PNG:
header("Content-type: image/png");
$im = imagecreatefrompng($this->file);
$white = imagecolorallocate($im, 255, 255, 255);
$textbox = imagettfbbox($this->size, 0, $this->font, $this->RandomText());
$px = ($type[0] - $textbox[4])/2;
$py = ($type[1] - $textbox[5])/2;
imagettftext($im, $this->size, $this->angel, $px, $py, $white, $this->font, $random);
imagepng($im);
imagedestroy($im);
break;
}
$_SESSION['code'] = $random;
}
}
$handle = new captcha();
if (!empty($_GET['length'])) $handle->setLength($_GET['length']);
if (!empty($_GET['font'])) $handle->setFont($_GET['font']);
if (!empty($_GET['size'])) $handle->setSize($_GET['size']);
if (!empty($_GET['angel'])) $handle->setAngel($_GET['angel']);
if (!empty($_GET['file'])) $handle->setFile($_GET['file']);
$handle->ConvertFontToImage();
?>
<?php
/**
* @author Dodit Suprianto
* Email: d0dit@yahoo.com
* Website: http://doditsuprianto.com, http://goiklan.co.nr
* Website: http://www.meozit.com, http://easyads.co.nr
*
* call captcha code <img src=captcha.class.php?length=&font=&size=&angel=&file=>
*
* You can enter the parameter or not. Explaination:
*
* length : how much character will be shown. 4 character by default.
* font : Type of font (ex. Arial.ttf). Georgia font by default
* size : Size of font character. 20 by default
* angel : angel of font character. 0 by default, it means horizontal position
* file : if you want subtitute the background image. "eyes.gif" by default.
*
* You must copy several file to current directory
* if you want to subtitute the default setting.
* such as font file, background file
*
*/
session_start();
?>
<html>
<head>
<title>Demo Captcha</title>
</head>
<body>
<?php
if ($_POST['submit'])
{
if ($_SESSION['code'] == $_POST['code'] && !empty($_SESSION['code']))
{
/**
* you should add other codes in here
* in this section, means that the captcha and code are match
*/
echo "captcha is correct";
unset($_SESSION['code']);
}
else
{
echo "captcha is wrong";
}
}
echo "<form action='$_SERVER[SELF]' method=POST>";
echo "Security code: <img src=captcha.class.php?length=4&font=&size=24&angel=5&file=><p>";
echo "Input code: <input type=text name=code><br>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form>";
?>
</body>
</html>