المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : برمجة نظام captcha "التحقق البشري!"



walid
11-01-2013, بتوقيت غرينيتش 04:25 PM
http://www.dzbatna.com/images/icons/idea.gif ط¨ط±ظ…ط¬ط© ظ†ط¸ط§ظ… captcha "ط§ظ„طھط*ظ‚ظ‚ ط§ظ„ط¨ط´ط±ظٹ!" (http://www.dzbatna.com/t2016700/)



السلآم عليكم ..!

اليوم قلت أشرح طريقة لكم درس مفصل مهم من دروس الحماية وهو نظام الـ captcha أو المعروف بالتحقق البشري !

^ عبارة عن نظام يكون رقم عشوائي على صورة ويتحقق من إدخال ما بداخل الصورة من أحرف وأرقام ~

الشرح طريقة رآح يكون عالسريع http://www.dzbatna.com/images/smilies/nosweat.gif

أولآ لنقوم بعمل فورم لإدخال البيانات "index.php" :

رمز PHP:

<code style="white-space:nowrap"> <code>
<s cript language="JavaS cript" type="text/javascri pt" src="ajax_captcha.js"></s cript>
<form id="frmCaptcha" nam e="frmCaptcha">
<table>
<tr>
<td align="left">
<label for="captcha">Captcha</label>
</td>
<td>
<input id="txtCaptcha" type="text" nam e="txtCaptcha" value="" maxlength="10" size="32" />
</td>



<td>
<img id="imgCaptcha" src="create_image.php" />
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input id="btnCaptcha" type="button" value="Captcha Test" nam e="btnCaptcha"
on click="getParam(docume nt.frmCaptcha)" />
</td>
</tr>
</table>

<div id="result">&nbsp;</div>
</form>
</code> </code>

كما تشاهدون في الملف أكواد جافا + كود code إستدعاء لملف أجاكس http://www.dzbatna.com/images/smilies/bigsmile.gif

^ إن وجدت مسافات قم بحذفها :cupidarrow:

الآن العمل الجدي ومهو ملف &quot;create_image.php&quot; والذي يقوم بتوليد الصورة الخاصة بالتحقق :

رمز PHP:

<code style="white-space:nowrap"> <code> <?php
//Start the session so we can store what the security code actually is
session_start();

//Send a generated image to the browser
create_image();
exit();

function create_image()
{
//Let's generate a totally random string using md5
// $md5_hash = md5(rand(0,999));
$len = 99;
$base='ABCDEFGHKLMNPQRSTWXYZ1234567890';
$max=strlen($base)-1;
$activatecode='';
mt_srand((double)microtime()*1000000);
while (strlen($activatecode)<$len+1)
$activatecode.=$base{mt_rand(0,$max)};

//We don't need a 32 character long string so we trim it down to 8
$security_code = substr($activatecode, 15, 8);

//Set the session to store the security code
$_SESSION&#91;"security_code"&#93; = $security_code;

//Set the image widt h and height
$widt h = 80;
$height = 20;

//Create the image resource
$image = ImageCreate($widt h, $height);

//We are making three colors, white, black and gray
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$grey = ImageColorAllocate($image, 204, 204, 204);

//Make the background black
ImageFill($image, 0, 0, $white);

//Add randomly generated string in white to the image
ImageString($image, 5, 4, 2, $security_code, $black);

//Throw in some lines to make it a little bit harder for any bots to break
// ImageRectangle($image,0,0,$widt h-1,$height-1,$grey);
// imageline($image, 0, $height/2, $widt h, $height/2, $grey);
// imageline($image, $widt h/2, 0, $widt h/2, $height, $grey);

//Tell the browser what kind of file is come in
header("Conten t-Type: image/jpeg");

//Output the newly created image in jpeg format
ImageJpeg($image);

//Free up resources
ImageDestroy($image);
}
?> </code> </code>

أولآ الملف بدأناه بـ جلسة جديدة لكي يكون التحقق بالكوكيز أي الجلسات http://www.dzbatna.com/images/smilies/bigsmile.gif

وقمت بعمل دالة تقوم بعمل رمز عشوائي ومكون من حروف وأرقام + لم أضف حرف الـ &quot;o&quot; لكي لآ يكون هناك أغلاط أو تشابه مع رقم الـ &quot;0&quot; الصفر !

هناك أيضا الطريقة التقليدية وهي تكوين الرمز بإستعمال تشفير الـ &quot;MD5&quot; ولكي تقوم باستمالها فقط أزل &quot;//&quot; قبلها كالتالي :

رمز PHP:

<code style="white-space:nowrap"> <code> $md5_hash = md5(rand(0,999));
</code> </code>

وأيضا يجب إزالة &quot;//&quot; من امام المتغير &quot;security_code&quot; لصبح كالتاالي :
رمز PHP:

<code style="white-space:nowrap"> <code>
// $security_code = substr($md5_hash, 15, 8);
$security_code = substr($activatecode, 15, 8);
</code> </code>

وفي البداية قلت رآح نتحقق عبر نظام الجلسات وهنا كود code يوضح كلآمي :

رمز PHP:

<code style="white-space:nowrap"> <code> $_SESSION&#91;"security_code"&#93; = $security_code;
</code> </code>

في الكود code السابق جعلنا الرمز العشوائي في الجلسة المسماه بإسم &quot;security_code&quot;

وأيضا هناك كود code عمل الصورة !

وأيضا بنها كود code قمت بحجبه وهو كود code لعمل خطوط على الأحرف ليكون التأكد أقوى ولآ أرى أن له لزمة ولتفعيها هذا نقوم بإزالة &quot;//&quot; من كل سطر كالتالي :

رمز PHP:

<code style="white-space:nowrap"> <code> ImageRectangle($image,0,0,$widt h-1,$height-1,$grey);
imageline($image, 0, $height/2, $widt h, $height/2, $grey);
imageline($image, $widt h/2, 0, $widt h/2, $height, $grey);
</code> </code>

الآن النظام كآمل والكمآل لله وحده !

نأتي لطريقة التأكد في الصفحات المراد حمايتها ولتكن باسم &quot;captcha&quot; :

رمز PHP:

<code style="white-space:nowrap"> <code>
<?php
//Continue the session
session_start();

//Make sure that the input come from a posted form. Otherwise quit immediately
if ($_SERVER&#91;"REQUEST_METHOD"&#93; <> "POST")
die("You can only reach this page by posting from the html form");

//Check if the security code and the session value are not blank
//and if the input text matches the stored text
if ( ($_REQUEST&#91;"txtCaptcha"&#93; == $_SESSION&#91;"security_code"&#93;) &&
(!empty($_REQUEST&#91;"txtCaptcha"&#93;) && !empty($_SESSION&#91;"security_code"&#93;)) ) {
echo "<h1>Test successful!</h1>";
} else {
echo "<h1>Test failed! Try again!</h1>";
}
?><?php
//Continue the session
session_start();

//Make sure that the input come from a posted form. Otherwise quit immediately
if ($_SERVER&#91;"REQUEST_METHOD"&#93; <> "POST")
die("You can only reach this page by posting from the html form");

//Check if the security code and the session value are not blank
//and if the input text matches the stored text
if ( ($_REQUEST&#91;"txtCaptcha"&#93; == $_SESSION&#91;"security_code"&#93;) &&
(!empty($_REQUEST&#91;"txtCaptcha"&#93;) && !empty($_SESSION&#91;"security_code"&#93;)) ) {
echo "<h1>Test successful!</h1>";
} else {
echo "<h1>Test failed! Try again!</h1>";
}
?> </code> </code>

كما هو وآضح قمنا بعمل جلسة جديدة !

وتحققنا إن كان طلب الصفحة كان عبر &quot;POST&quot; وليس &quot;GET&quot; بهذا الكود code :

رمز PHP:

<code style="white-space:nowrap"> <code> if ($_SERVER&#91;"REQUEST_METHOD"&#93; <> "POST")
die("You can only reach this page by posting from the html form");
</code> </code>

الآن نقوم بالتحقق إن كان تم إدخال الرموز في الحقل المخصص وهذا المدخل يساوي الجلسة المخصصة التي قمنا بها !
وأيضا إن كان الحقلآن غير فارغان وأقصد بهما حقل الجلسة وحقل الرمز للتحقق !!

وهذا هو الكود code :

رمز PHP:

<code style="white-space:nowrap"> <code> if ( ($_REQUEST&#91;"txtCaptcha"&#93; == $_SESSION&#91;"security_code"&#93;) &&
(!empty($_REQUEST&#91;"txtCaptcha"&#93;) && !empty($_SESSION&#91;"security_code"&#93;)) )
</code> </code>


الآن قمنا بعمل كود code التحقق وإن تم تحقق الشرط إطبع التالي :

رمز PHP:

<code style="white-space:nowrap"> <code> <h1>Test successful!</h1>
</code> </code>

وإن لم تتحقق الشروط إطبع التالي :

رمز PHP:

<code style="white-space:nowrap"> <code> <h1>Test failed! Try again!</h1>
</code> </code>

جميل الآن إنتهينا من كل شي تقريبا وبقي ملفات الجافا !

طبعا لن أقوم بشرح طريقةها ولآكن طريقة عملها تقوم بجلب صفحة التحقق والصورة بعد كل تحقق http://www.dzbatna.com/images/smilies/nosweat.gif

الملف &quot;ajax_captcha.js&quot; :

رمز PHP:

<code style="white-space:nowrap"> <code> //Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
if (windo w.XMLHttpRequest) {
return new XMLHttpRequest(); //Mozilla, Safari ...
} else if (windo w.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP"); //IE
} else {
//Display our error message
ale rt("Your browser doesn't support the XmlHttpRequest object.");
}
}
//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();
//Initiate the AJAX request
function makeRequest(url, param) {
//If our readystate is either not started or finished, initiate a new request
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
//Set up the connection to captcha_test.html. True sets the request to asyncronous(default)
receiveReq.open("POST", url, true);
//Set the function that will be called when the XmlHttpRequest objects state changes
receiveReq.onreadystatechange = updatePage;

//Add HTTP headers to the request
receiveReq.setRequestHeader("Conten t-type", "application/x-www-form-urlencoded");
receiveReq.setRequestHeader("Conten t-length", param.length);
receiveReq.setRequestHeader("Connection", "close");

//Make the request
receiveReq.send(param);
}
}
//Called every time our XmlHttpRequest objects state changes
function updatePage() {
//Check if our response is ready
if (receiveReq.readyState == 4) {
//Set the conten t of the DIV element with the response text
documen t.getElementById('result').innerHTML = receiveReq.responseText;
//Get a reference to CAPTCHA image
img = documen t.getElementById('imgCaptcha');
//Change the image
img.src = 'create_image.php?' + Math.random();
}
}

//Called every time when form is perfomed
function getParam(theForm) {
//Set the URL
var url = 'captcha.php';
//Set up the parameters of our AJAX call
var postStr = theForm.txtCaptcha.nam e + "=" + encodeURIComponent( theForm.txtCaptcha.value );
//Call the function that initiate the AJAX request
makeRequest(url, postStr);
}
</code> </code>

قم بحفظة في نفس المجلد !

الآن قم بإستعرآض الملف &quot;index.php&quot; وأخبرني برأيك في نظام التحقق http://www.dzbatna.com/images/smilies/bigsmile.gif

مهم يوجد مسافات ببعض الكلمات والسبب يعود للفلترة في الموقع وقريبا سوف أقوم بإلغائها لنعطي المجآل للمبرمجين :d

الاكواد شبه منقولة .. أتمنى حفظ الحقوق عند النشر !


HaNniBaL KsA
www.r00t-s3c.com (http://www.r00t-s3c.com)






ألعاب الأندرويد مجانا و حصريا (http://www.apotox.info/forum)




https://fbcdn-sphotos-d-a.akamaihd.net/hphotos-ak-ash4/482113_236967293114455_1193518507_n.png (http://www.dzbatna.com)
©المشاركات المنشورة تعبر عن وجهة نظر صاحبها فقط، ولا تُعبّر بأي شكل من الأشكال عن وجهة نظر إدارة المنتدى (http://www.dzbatna.com)©

استعمل مربع البحث في الاسفل لمزيد من المواضيع


سريع للبحث عن مواضيع في المنتدى