js ile uygulanmış bir captcha uygulaması kullanımı çok kolay ve sorunsuz..
öncelikle form bileşenimizin sonuna:
CODE:
name="capform" onSubmit="return capCheck(this);"
kodumuzu ekliyoruz..
sora güvenlik kodumuzu oluşturuyoruz..
CODE:
<a href="http://www.adres.com/" target="_out"><img src="captcha/image.php" name="capimage" border="0"></a><BR /><BR />
Enter text from image above into the box below:<BR />
<input type="text" value="" name="captext" /></textarea>
işte buda js kodumuz bunun sayesinde php ile sorgudn kurtuluyoruz..bunu istediğimiz yere koyuyoruz.
CODE:
<script id="clientEventHandlersJS" language="javascript">
<!--
function capCheck(theForm) {
var xmlhttp;
var sReply;
DataToSend = "action=process&captext=" + escape(theForm.captext.value);
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST",'captcha/process.php',false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(DataToSend);
sReply = xmlhttp.responseText;
} else if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("POST",'captcha/process.php',false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(DataToSend);
sReply = xmlhttp.responseText;
}
if (sReply == "good") {
alert("Kod doğru"); return true;
} else if (sReply == "bad") {
alert("Kod yanlış"); return false;
} else if (sReply == "") {
alert("Giriş yapılmadı"); return false;
} else {
alert("Hata"); return false;
}
}
-->
</script>
image.php içeriği:
CODE:
<?
session_start();
//Set these avriables as you see fit
$fontsize = 12;
$numcharacters = 8;
$bgcolor = "ffffff";
$textcolor = "000000";
$num_interference_lines = 0;
//At least one of these MUST be true
$str_includes_numbers = true;
$str_includes_uppercase = true;
$str_includes_lowercase = true;
//Generate Captcha String
$_SESSION['teassocaptcha'] = RandString($numcharacters, $str_includes_numbers, $str_includes_uppercase, $str_includes_lowercase);
//Send a generated image to the browser
create_image($numcharacters, $fontsize, $bgcolor, $textcolor, $num_interference_lines);
exit();
function create_image($length, $fontsize, $bgcolor, $textcolor, $numlines)
{
//Set the image width and height
$trifont = 2* $fontsize;
$width = ($trifont * $length);
$height = 4.2* $fontsize;
//Create the image resource
$image = imagecreate($width, $height);
//We are making colors
$forecolor = imagecolorallocate($image, hexdec(substr($textcolor,0,2)), hexdec(substr($textcolor,2,2)), hexdec(substr($textcolor,4,2)));
$backcolor = imagecolorallocate($image, hexdec(substr($bgcolor,0,2)), hexdec(substr($bgcolor,2,2)), hexdec(substr($bgcolor,4,2)));
//Make the background backcolor
imagefill($image, 0, 0, $backcolor);
//Add randomly generated string in forecolor to the image
//imagestring($image, 7, 30, 3, $pass, $forecolor);
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//imageloadfont("chick.ttf");
for($i = 0; $i < $length; $i++) {
$source = imagecreate($trifont, $trifont);
//We are making colors
$forecolors = imagecolorallocate($source, hexdec(substr($textcolor,0,2)), hexdec(substr($textcolor,2,2)), hexdec(substr($textcolor,4,2)));
$backcolors = imagecolorallocate($source, hexdec(substr($bgcolor,0,2)), hexdec(substr($bgcolor,2,2)), hexdec(substr($bgcolor,4,2)));
imagefill($source, 0, 0, $backcolors);
//imagestring($source, 5, 3, 0, substr($_SESSION['teassocaptcha'], $i, 1), $forecolors);
imagettftext($source, $fontsize, 0, 0, 18, $forecolors, "verdana.ttf", substr($_SESSION['teassocaptcha'], $i, 1));
//Rotate Somehow
switch(mt_rand(1,8)) {
case 1:
$source = imagerotate($source, -20, $backcolors);
break;
case 2:
$source = imagerotate($source, 20, $backcolors);
break;
case 3:
$source = imagerotate($source, -15, $backcolors);
break;
case 4:
$source = imagerotate($source, 15, $backcolors);
break;
case 5:
$source = imagerotate($source, -10, $backcolors);
break;
case 6:
$source = imagerotate($source, 10, $backcolors);
break;
case 7:
$source = imagerotate($source, -2, $backcolors);
break;
case 8:
$source = imagerotate($source, 2, $backcolors);
break;
}
//Set Location variables
$loc_x = 5+($trifont*$i);
$loc_y = $trifont/2 + mt_rand((-1 * $fontsize/10),($fontsize/10));
//Set Size Variables
$size_x = $trifont * (mt_rand(100, 110) / 100);
$size_y = $trifont * (mt_rand(100, 110) / 100);
imagecopymerge($image,$source,$loc_x,$loc_y,0,0,$size_x,$size_y,90);
imagedestroy($source);
}
/*Throw in some lines to make it a little bit harder for any bots to break */
imagesetstyle($image, array($forecolor, $forecolor, $forecolor, $forecolor, $forecolor, $forecolor, $forecolor, $forecolor, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT));
imagerectangle($image,0,0,$width-1,$height-1,$forecolor);
//for($i = 1; $i < mt_rand(1,5); $i++) {
for($i = 1; $i < (1 + $numlines); $i++) {
$from_x = mt_rand(0, $width);
$from_y = mt_rand(0, $height);
$to_x = mt_rand(0, $width);
$to_y = mt_rand(0, $height);
imageline($image, $from_x, $from_y, $to_x, $to_y, IMG_COLOR_STYLED);
}
//Output the newly created image in jpeg format
imagejpeg($image);
//Free up resources
imagedestroy($image);
}
function RandString($len, $num, $uc, $lc) {
if (!$len || $len<1 || $len>100) {
print "Error: \"Length\" out of range (1-100)<br>n";
return;
}
if (!$num && !$uc && !$lc) {
print "Error: No character types specified<br>n";
return;
}
$s="";
$i=0;
do {
switch(mt_rand(1,3)) {
// get number - ASCII characters (0:48 through 9:57)
case 1:
if ($num==1) {
$s .= chr(mt_rand(48,57));
$i++;
}
break;
// get uppercase letter - ASCII characters (a:65 through z:90)
case 2:
if ($uc==1) {
$s .= chr(mt_rand(65,90));
$i++;
}
break;
// get lowercase letter - ASCII characters (A:97 through Z:122)
case 3:
if ($lc==1) {
$s .= chr(mt_rand(97,122));
$i++;
}
break;
}
} while ($i<$len);
return $s;
}
?>
process.php içeriği:
CODE:
<?
session_start();
if (strtolower($_POST['captext']) == strtolower($_SESSION['teassocaptcha']))
echo "good";
else
echo "bad";
?>
güle güle kullanın..