کد هایی که توی کلاس کپچا استفاده می کنم رو در ادامه قرار دادم.
هیچ مشکلی توی کپچا و ذخیره سسشن هایی که توی کپچا ایجاد می شن وجود نداره و سسشن ها بخوبی توی دیتابیس ذخیره می شن. مشکل اینجاست که وقتی سسشن های اسکریپت کپچا توی دیتابیس ذخیره می شن، بقیه سسشن ها به کلی پاک می شن.
<?php
class captch_file
{
private $font;
private $width;
private $height;
private $characters;
function __construct()
{
define('ROOT',dirname(__FILE__));
define('DS',DIRECTORY_SEPARATOR);
define('SES_PANEL','yes');
define('SES_INC','yes');
require_once('config.php');
require_once('mysessions.php');
$this->db = new mysqli(IHOST,IUSER,IPASS,IDATABASE) or die ("امکان انجام درخواست شما وجود ندارد، لطفا مجددا تلاش کنيد");
$this->db->set_charset('UTF8');
$session=new MySessionHandler(IHOST,IUSER,IPASS,IDATABASE,$this->db);
session_start();
$this->font='RosewoodStd-Regular.otf';
$this->width = isset($_GET['width']) && $_GET['width'] < 600 ? $_GET['width'] : '145';
$this->height = isset($_GET['height']) && $_GET['height'] < 200 ? $_GET['height'] : '40';
$this->characters = isset($_GET['characters']) && $_GET['characters'] > 2 ? $_GET['characters'] : '6';
}
private function generateCode($charsnum)
{
/* list all possible characters, similar looking characters and vowels have been removed */
$possible = '23456789bcdfghjkmnpqrstvwxyz';
$code = '';
$i = 0;
while ($i < $charsnum)
{
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}
private function CaptchaSecurityImages()
{
$code = $this->generateCode($this->characters);
/* font size will be 75% of the image height */
$font_size = $this->height * 0.70;
$image = imagecreate($this->width, $this->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, 100, 120, 180);
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($this->width - $textbox[4])/2;
$y = ($this->height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
$_SESSION[mycaptcha_code]="$code";
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
}
public function runcaptcha()
{
$this->CaptchaSecurityImages();
}
}
$captcha = new captch_file();
$captcha->runcaptcha();
?>