หลักการของCAPCHAที่ผมได้สอนนี้ เราจะเขียนคำสั่งPHPสุ่มตัวอักษรผสมตัวเลขขึ้นมา 5 ตัว และใช้ฟังชั่นเกี่ยวกับรูปภาพของPHP(imagecreatefromjpeg, imagecolorallocate, imagettftext) เพื่อดึงภาพBackgroundขึ้นมาด้วยฟังก์ชั่น imagecreatefromjpeg แล้วนำตัวอักษรมาสลับสีตัวอักษรด้วยฟังก์ชั่น imagecolorallocate แล้วเขียนตัวอักษรลงบนภาพBackgroundด้วยฟังก์ชั่น imagettftext และเราจะเขียนคำสั่งJavascript เพื่อเปลี่ยนรหัสCAPCHAใหม่ด้วยครับ
อันดับแรกให้สร้างไฟล์.phpขึ้นมา 3 ไฟล์ คือ image.php, capcha.php,chkcapcha.php
ไฟล์ image.php ->สำหรับแสดงผลรูปภาพCAPCHA ซึ่งเราจะใช้PHPสร้างรหัสและสุ่มรหัสขึ้นมาและแปลงให้เป็นไฟล์รูปภาพ ให้พิมพ์คำสั่งดังนี้
<?php session_start(); $img=imagecreatefromjpeg("bg.jpg"); $fonts="fonts/courbd.ttf";//พาธของรูปแบบตัวอักษร ผมใช้ตัวอักษรแบบ courbd $_SESSION['secure_capcha']=''; $key = "12345678910abcdefghijklmnopqrstuvwxysABCDEFGHIJKLMNOPQRSTUVWXYZ"; $security_cap=''; srand((double)microtime()*1000000); for($i=0; $i<5; $i++) { $security_cap.= $key[rand()%strlen($key)]; //สุ่มตัวเลขและตัวอักษรจำนวน 5 ตัว } $image_text=$security_cap; $_SESSION['secure_capcha']=$security_cap; $red=rand(100,255); $green=rand(100,255); $blue=rand(100,255); $text_color=imagecolorallocate($img,255-$red,255-$green,255-$blue); //int imagecolorallocate ( resource $image , int $red , int $green , int $blue ) $text=imagettftext($img,16,0,rand(10,30),rand(25,35),$text_color,$fonts,$image_text); //imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text ) //$img คือที่อยุ่ของbackgroundรูปภาพ //16 คือขนาดตัวอักษร // 0 คือมุมสำหรับแสดงผลแบบแนวนอน สามารถกำหนดการแสดงผลถึง 0-180 องศา // rand(10,30) ระยะห่างจากแนวนอน(x)แบบสุ่มแสดงผลจาก 10px-30px // rand(25,35) ระห่างจากแนวตั้ง(y)แบบสุ่มแสดงจาก 25px - 35px //$text_color สีตัวอักษรแบบสุ่ม header("Content-type:image/jpeg"); header("Content-Disposition:inline ; filename=secure.jpg"); imagejpeg($img); ?>
ไฟล์ capcha.php ->สำหรับสร้างฟอร์มกรอกข้อมูลรหัสป้องกันสแปม ให้เขียนคำสั่งดังนี้
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>ตัวอย่างการใช้งานรหัสป้องกันสแปม</title> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $_CapchaChange=function(IdCap){//Parameter "IdCap" คือ ชื่อidของรูปภาพCapcha IdCap.src="image.php?cid="+Math.random()*20; } </script> </head> <body> <img src="image.php" id="tcapcha" alt="แค๊ปช่า" /> <input type="button" name="changpass" id="changpass" value="เปลี่ยนรหัส" onclick="$_CapchaChange(tcapcha)" /> <form id="form1" name="form1" method="post" action="chkcapcha.php" target="_blank">กรอกรหัสที่เห็น <input type="text" name="cap_input" id="cap_input" /> <input type="submit" name="btsubmit" id="btsubmit" value="ตกลง"/> </form> </body> </html>
ไฟล์ chkcapcha.php ->สำหรับตรวจเช็ครหัสที่เราป้อนในช่องกรอกข้อมูลว่าตรงกับรหัสCAPCHAหรือไม่ ให้พิมพ์คำสั่งดังนี้
<?php session_start(); header ('Content-type: text/html; charset=utf-8'); if($_POST['cap_input']!=''){ if($_SESSION['secure_capcha']!=$_POST['cap_input']){ echo 'รหัสไม่ถูกต้อง'; }else{ echo 'รหัสถูกต้อง'; } }else{ echo 'คุณยังไม่ได้ระบุรหัสผ่าน'; } ?>
นี้เป็นแค่ตัวอย่างแบบง่ายๆครับ หวังว่าท่านคงจะนำไปประยุกต์ใช้งานกับเว็บไซต์ของท่านได้นะครับ ติดปัญหาตรงไหนสามารถสอบถามผมได้จากช่องCommentด้านล่างครับ
DEMO-> http://panuwat.orgfree.com/capcha/capcha.php
DOWNLOAD-> http://www.4shared.com/rar/TohCOwtz/capcha.html
ตัวอย่างการใช้งานในเว็บบอร์ดของผมครับ
pass อาไรอะงับ
ตอบลบศึกษาเพิ่มเติมได้จากที่นี่ครับ->http://meewebfree.com/site/something-in-the-world/78-what-is-captcha
ตอบลบขอบคุณครับ
ตอบลบไฟล์imageมันfailอะครับ ต้องแก้ไงครับ
ตอบลบWarning: imagecreatefromjpeg(bg.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /home/content/74/8750574/html/image.php on line 3
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/content/74/8750574/html/image.php on line 17
Warning: imagettftext() expects parameter 1 to be resource, boolean given in /home/content/74/8750574/html/image.php on line 19
Warning: Cannot modify header information - headers already sent by (output started at /home/content/74/8750574/html/image.php:3) in /home/content/74/8750574/html/image.php on line 27
Warning: Cannot modify header information - headers already sent by (output started at /home/content/74/8750574/html/image.php:3) in /home/content/74/8750574/html/image.php on line 28
Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/content/74/8750574/html/image.php on line 29
ปัญหาน่าจะอยู่ที่คุณได้ย้ายไฟล์(image.php)
ลบไปไว้ในโฟลเดอร์ของตัวเองและระบุพาธของไฟล์(image.php) ไม่ถูกต้องครับ
ปล.ถ้าจะย้ายไฟล์ image.php ต้องย้ายไฟล์ bg.jpg,โฟลเดอร์ fonts ตามไปด้วยนะครับ
pass word ที่ใช้ โหลด ไฟล์ อะไร ครับบ รบกวนหน่อย
ตอบลบผมก็ไม่รู้ว่าต้องใส่รหัสผ่านอะไรอะครับ
ลบเมื่อก่อนก็โหลดได้อยู่นะครับ แต่อยู่ดีดีให้ใส่รหัสผ่านเฉยเลย และผมก็ไม่ได้ใส่รหัสผ่านอะไรเลยครับ
4shared อาจกำลังแก้ไขระบบอยู่มั้งครับ
ลบ