php coded by vovka pw class Captcha protected code const KEY captcha_c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
//coded by vovka.pw
class Captcha {
protected $code;
const KEY = "captcha_code";
public function __construct($check = false, $length = 5, $width = 800, $height = 300) {
if (!$check) {
$this->code = $this->genCode($length);
$this->setSession();
$this->draw($width, $height);
}
}
protected function genCode($length) {
if($length <= 0)
return 'error';
$code = '';
$chars = "0123456789";
for($i = 0; $i < $length; $i++)
$code .= $chars{rand(0, strlen($chars) - 1)};
return $code;
}
protected function setSession() {
$_SESSION[self::KEY] = $this->code;
}
public function check($code) {
$flag = isset($_SESSION[self::KEY]) && (String) $code == $_SESSION[self::KEY];
unset($_SESSION[self::KEY]);
return $flag;
}
protected function draw($width, $height) {
$img = imagecreatetruecolor($width, $height);
imagefilledrectangle($img, 0, 0, $width, $height, 0xDDDDDD);
imagefilledrectangle($img, 0, $height - ($height * 0.25), $width, $height, 0x000000);
$coords = imagettfbbox($height * 0.18, 0, 'font.ttf', $_SERVER['HTTP_HOST']);
imagettftext($img, $height * 0.18, 0, round($width / 2) - round(($coords[2] - $coords[0]) / 2), $height - ($coords[1] - $coords[6]) * 2, 0xFFFFFF, 'font.ttf', $_SERVER['HTTP_HOST']);
imagepng($img);
}
}