<?php //validate.php
if (session_id() == ""){ //ако не е стратирана сесия, в противен случай ще я изпразни
session_start();
}
function createvalidcode($numChars=4, $width=125, $height=28, $circle=20) {
$chars = range('a', 'z'); // използваните знаци
$showChar = ''; // ако си с Е_STRICT на error_reporting без този ред ще ти даде грешка
for($i=0;$i<$numChars;$i++){
$case = rand(0, 10);
$char = rand(0, (count($chars)-1) ); // случаен елемент от масива
$showCase = ($case >= 7)?strtoupper($chars[$char]):$chars[$char]; // 30% шанс за uppercase
$showChar .= $showCase; // добавяне към стринг-а
}
$_SESSION['validationCode'] = $showChar;
$img = imagecreate($width,$height);
$background = imagecolorallocate($img, 250, 250, 250);
$gray = imagecolorallocate($img, 230, 230, 230);
$text = imagecolorallocate($img, 200, 200, 200);
for($i=1;$i<=$circle;$i++){
imagearc($img, rand(0,$width), rand(0,$height), rand(0,200), rand(0,200), 0, 360, $gray);
}
for($i=1;$i<=$circle;$i++){
imagearc($img, rand(0,$width), rand(0,$height), 6, 6, 0, 360, $gray);
}
imagestring($img,5,14,6,implode(" ", str_split($showChar)), $text);
header('Content-type: image/png'); // казваме на браузъра, че е картинка
imagepng($img);
imagedestroy($img);
}
function validateSentCode($code){
if(isset($_SESSION['validationCode']) && $_SESSION['validationCode'] == $code){
unset($_SESSION['validationCode']);
return true;
}else{
return false;
}
}
?>