استخدام وبرمجة رمز التحقق captcha بلغة php

محتويات المقال:
استخدام وبرمجة رمز التحقق captcha بلغة php


رمز التحقق captcha هي نظام بسيط للتحقق من المستخدم ما إذا كان انسان او روبوت ، وتستخدم للحماية من السبام spam على مواقع الويب، حيث يتم انشاء صورة والتي تتكون من نصوص عشوائية بالإضافة الى مجموعة من الخطوط والنقاط العشوائية للتشويش ودمج النص في الصورة، ويتم استخدامها في النماذج Forms بشكل كبير وخاصة مع صندوق الاتصال والتعليقات.

يتم تخزين الكود او النص العشوائي في جلسة session قبل دمجها في الصورة وارسالها للمستخدم ، وعند ارسال رمز التحقق من المستخدم يتم مقارنته مع القيمة المخزنة في الجلسة ، وهذه هي الية عملها.

هذا كود جاهز لإنشاء الكابتشا captcha ، حيث يمكنك تغيير الخط المستخدم ولون الخط وابعاد الصورة والحروف المستخدمة في نظام التحقق ، بالإضافة الى التحكم بالضوضاء (خطوط ونقاط) وكميتها في الصورة ، كما هو محدد باللون الأحمر في في الكود التالي :
<?php
session_start();
//Settings: You can customize the captcha here
$image_width = 130;
$image_height = 50;
$characters_on_image = 6;
$font = './monofont.ttf';

//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 0;
$random_lines = 30;
$captcha_text_color="0xffffff"
$captcha_noice_color = "0xffffff"

$code = '';


$i = 0;
while ($i < $characters_on_image) { 
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}


$font_size = $image_height * 0.75;
$image = @imagecreate($image_width, $image_height);


/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 40, 40, 40);

$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'], 
		$arr_text_color['green'], $arr_text_color['blue']);

$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'], 
		$arr_noice_color['green'], $arr_noice_color['blue']);


/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
 mt_rand(0,$image_height), 2, 3, $image_noise_color);
}


/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
 mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}


/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code); 
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);


/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
$_SESSION['6_letters_code'] = $code;

function hexrgb ($hexstr)
{
  $int = hexdec($hexstr);

  return array("red" => 0xFF & ($int >> 0x10),
               "green" => 0xFF & ($int >> 0x8),
               "blue" => 0xFF & $int);
}
?>

حيث تم بدء جلسة في بداية الكود ، وكما هو ملاحظ في الكود السابق المحدد باللون الأخضر تم تخزين كود التحقق في الجلسة باسم 6_letters_code ، لكي يتم مقارنتها مع الكود المرسل من المستخدم .

في الكود التالي تم استدعاء الكابتشا والتحقق من البيانات المدخلة ، ومقارنتها مع القيمة المخزنة في الجلسة :
<?php
session_start();
$errors ="";
if(isset($_POST['submit'])){
if(empty($_SESSION['6_letters_code'] ) ||
    strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
  {
    $errors = " رمز التحقق غير صحيح";
}else{
  header('Location: thank-you.html');
}
}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP Secure Advanced Captcha.</title>
<link href="./styles.css" rel="stylesheet">
<script type='text/javascript'>
function refreshCaptcha(){
	var img = document.images['captchaimg'];
	img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}
</script>

</head>
<body>
<?php
if(!empty($errors)){
echo "<p class='err'>".$errors."</p>";
}
?>
<div class="container">
<div id="header" class="header">
  <h1>PHP Secure Advanced Captcha</h1>
  <p> <a href="http://www.dev-techno.com/">www.dev-techno.com</a></p>
</div>


<form method="POST" name="contact_form" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"  class="dev-techno" >
     <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' >
     <p> ادخل رمز التحقق هنا:</p>
     <input type="text" id="6_letters_code" name="6_letters_code"/><br/>
      <small>لا أستطيع قراءة الرمز؟ اضغط <a href='javascript: refreshCaptcha();'>هنا</a> للتحديث</small> <br/>
     <input type="submit" name="submit" value="ارسال" />
     

</form>
</div>
</body>
</html>

تم استدعاء الصورة التي تحتوي على كود الكابتشا بالطريقة الآتية :
<img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' >

في الكود التالي تم التحقق من صحة البيانات المدخلة بمقارنتها مع البيانات المخزنة في الجلسة 6_letters_code ما إذا كانت صحيحة ، بحيث اذا كانت متطابقة يتم توجيهك الى الصفحة المقصود ه او اكمال عملية ارسال البيانات ، وفي حال عدم التطابق يتم عرض رسالة الخطأ.
if(empty($_SESSION['6_letters_code'] ) ||
    strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
  {
    $errors = " رمز التحقق غير صحيح";
}else{
  header('Location: thank-you.html');
}

أنشئت مثال كامل لاستخدام الكابتشا captcha في مواقع الويب ، يمكنك تحميله من هنا :
تحميل الكود
كانت هذه طريقة سهلة لإستخدام رمز التحقق captcha في مواقع الويب ، واذا واجهت أي مشكلة او لديك استفسار حول الموضوع لا تتردد بتركها تعليق ، دمتم في رعاية الله وحفظه!

اقرأ ايضا:
رفع عدة ملفات او صور بنفس الوقت بلغة php
الكلمات المفتاحية: