ประเดิมบอร์ดใหม่ครับ เขียน Captcha มาให้ใช้กัน เอาไว้กัน Spam ครับ
file: captcha.class.php
<?php
class captcha {
var $str;
var $width = 55;
var $height = 20;
var $transparent = false;
var $bg = '#EEEEEE';
var $color = '#666666';
var $len = 5;
var $size = 5;
var $left = 5;
var $top = 2;
var $type = 'image/gif';
function draw($text='') {
$this->str = ($text == '') ? $this->random() : $text;
$this->show($this->create($this->str));
}
function random(){
$string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$string = str_repeat($string,3);
$string = str_shuffle($string);
srand();
$from = rand(0,strlen($string)-$this->len);
$code = substr($string,$from,$this->len);
return $code;
}
function color2rgb($color) {
if ($color[0] == '#')
$color = substr($color, 1);
if (strlen($color) == 6)
list($r, $g, $b) = array($color[0].$color[1], $color[2].$color[3], $color[4].$color[5]);
elseif (strlen($color) == 3)
list($r, $g, $b) = array($color[0], $color[1], $color[2]);
else
return false;
$r = hexdec($r);
$g = hexdec($g);
$b = hexdec($b);
return array($r, $g, $b);
}
function create($text) {
$rgb1 = $this->color2rgb($this->bg);
$rgb2 = $this->color2rgb($this->color);
$image = imagecreate($this->width, $this->height);
$bg = imagecolorallocate($image, $rgb1[0], $rgb1[1], $rgb1[2]);
$textcolor = imagecolorallocate($image, $rgb2[0], $rgb2[1] , $rgb2[2]);
imagestring($image, $this->size, $this->left, $this->top, $text, $textcolor);
if ($this->transparent)
imagecolortransparent($image, $bg);
return $image;
}
function show($image) {
header('content-type: '.$this->type);
imagegif($image);
imagedestroy($image);
}
function getstr() {
return $this->str;
}
}
?>
File: img.php
<?php
session_start();
require ("captcha.class.php");
$captcha = new captcha();
$captcha->transparent = true;
$captcha->draw();
$_SESSION['captcha'] = $captcha->getstr();
?>
How to use
<img src="img.php?=1234" onclick="this.src=this.src+'&'+Math.random();" border="1" align="absmiddle" />
ตรง 1234 ไปเขียน function random เอาเองนะ
เอาไปใช้ดูครับ หวังว่าคงช่วยได้บ้างนะ