Merhaba. Spam sorununa çözüm olarak kullanılan birçok uygulama var. En yaygın olanı tabi ki reCAPTCHA. İlerleyen süreçte bununla da alakalı bir makale yazmayı düşünüyorum. Bugün, PHP yardımıyla kullanıcı etkileşimli captcha mantığı nasıl oluşturulur bundan bahsedeceğim.
Öncelikle bir session oluşturulmalı.
session_start();
İkinci olarak, Matematiksel Captcha için kullanacağımız sayıları üretiyoruz:
$num1=rand(1,9); //1 ile 9 arasında rasgele sayı üretir. $num2=rand(1,9); //1 ile 9 arasında rasgele sayı üretir. $captcha_total=$num1+$num2;
Üçüncü olarak, Matematik sorusunu bir değişkene aktardık.
$math = "$num1"." + "."$num2"." =";
Dördüncü aşama, Match Captcha görselinin özelliklerini belireyelim.
$image = imagecreatetruecolor(120, 30); //Görsel boyutunu sayılardan değiştirebilirsiniz $black = imagecolorallocate($image, 0, 0, 0); $color = imagecolorallocate($image, 0, 100, 90); $white = imagecolorallocate($image, 0, 26, 26);
Son olarak görseli oluşturalım.
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 20, 0, 20, 25, $color, $font, $math );//Font boyutunu sayılardan değiştirebilirsiniz
header("Content-type: image/png");
imagepng($image);
captcha.php (Tümü)
session_start();
$num1=rand(1,9); //Generate First number between 1 and 9
$num2=rand(1,9); //Generate Second number between 1 and 9
$captcha_total=$num1+$num2;
$math = "$num1"." + "."$num2"." =";
$_SESSION['rand_code'] = $captcha_total;
$font = 'mmobuyit-captcha-fonts/Arial.ttf';
$image = imagecreatetruecolor(120, 30); //Görsel boyutunu sayılardan değiştirebilirsiniz.
$black = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 0, 100, 90);
$white = imagecolorallocate($image, 0, 26, 26);
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 20, 0, 20, 25, $color, $font, $math );//Font boyutunu sayılardan değiştirebilirsiniz.
header("Content-type: image/png");
imagepng($image);
Kontrol Mekanizması
$errors = array();
if ($_POST['registerUser']) {
if (empty($errors) === true) {
if (empty($_POST['captcha_entered'])) {
$errors[] = '<p class = "input-error">Lütfen Captcha sorusuna cevap verin!</p>';
} elseif ($_REQUEST['captcha_entered']!=$_SESSION['rand_code']) {
$errors[] = '<p class = "input-error">Matematik sorusu cevabınız yanlış</p>';
}
}
}
Form içerisinde kullanımı (Örnek)
<form method="post" class="register" id = "register-ajax">
<?php
if (empty($_POST) === false && empty($errors) === true) {
echo 'Success';
//Buraya başarılı işlem kodları...
} else if (empty($errors) === false) {
echo output_errors($errors);
}
?>
<p>
<img src="captcha.php" />
<input name="captcha_entered" style = "width: 100px !important;" type="text" id="captcha_entered" size="5" maxlength="2" placeholder = "Answer" />
<input type="hidden" name="captcha_total" id="captcha_total" value="<?php echo $_SESSION['rand_code']; ?>" />
</p>
<p>
<input type="submit" class="button" name="registerUser" value="<?php _e( 'Register' ); ?>" />
</p>
</form>