เขียน Captcha (Image Verify) ป้องกัน bot Auto post
บทความนี้ผม ใช้ Zend Framework 1.5 (เป็นเวอร์ชั่นเก่าที่ยังไม่มี Zend_Captcha)
การใช้งาน สมมุติเรามี Application ที่เขียนบน Zend Framework อยู่แล้ว
การทำ Image Verify ต้องใช้ Zend_Session_Namespace ด้วย ดังนั้น ตอนที่เราสร้าง Class สำหรับทำ Image Verify เราจำเป็นต้อง Load Class นี้ เข้ามาใช้งานด้วย
ตามโค้ดนี้
Zend_Loader::loadClass('Zend_Session_Namespace');
ส่วนโค้ดทั้งหมด
class ImagegeneratorController extends Zend_Controller_Action
{
public function init()
{
$this->_helper->viewRenderer->setNoRender();
}
public function generatecaptchaAction()
{
Zend_Loader::loadClass('Zend_Session_Namespace');
$defaultNamespace = new Zend_Session_Namespace('code_verify');
$md5_hash = md5(rand(0,999));
$security_code = substr($md5_hash, 15, 5);
$defaultNamespace->security_code = $security_code;
//Set the image width and height
$width = 100;
$height = 20;
$image = ImageCreate($width, $height);
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$grey = ImageColorAllocate($image, 204, 204, 204);
ImageFill($image, 0, 0, $black);
ImageString($image, 3, 30, 3, $security_code, $white);
ImageRectangle($image,0,0,$width-1,$height-1,$grey);
imageline($image, 0, $height/2, $width, $height/2, $grey);
imageline($image, $width/2, 0, $width/2, $height, $grey);
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image);
}
}
-----------------------------------------
และใช้โค้ดนี้วางใน form (ไฟล์.phtml) เพื่อให้แสดงรูปจาก Class ข้างบน
< img src="baseUrl; ?>/imagegenerator/generatecaptcha" />
ส่วนวิธีตรวจสอบว่าคนกรอกกรอก Code ถูกหรือไม่ ก็ตรวจสอบตามนี้
$Code = $filter->filter($this->_request->getPost('Code'));
$defaultNamespace = new Zend_Session_Namespace('code_verify');
if($Code != $defaultNamespace->security_code) {
// โค้ดไม่ถูก
} else {
// work
}
บอร์ด php : http://www.mindphp.com/forums/index.php
- คุณอยู่ที่:
- นักพัฒนา
- Pear Zend Framework
- เขียน Captcha (Image Verify) ป้องกัน bot Auto post
VDO บทเรียน live
สอนเขียนโปรแกรม
- บทเรียน MySQL Store Procedure (14)
- บทเรียน PostgreSQL Stored Procedures (5)
- บทเรียน Phalcon Framework (0)
- บทเรียน Python Framework Flask (21)
- บทเรียน HTML (39)
- บทเรียน HTML5 (31)
- บทเรียน CSS (64)
- บทเรียน Bootstrap (7)
- บทเรียน สอนเขียน php พื้นฐาน (49)
- บทเรียน ReactJS (13)
- บทเรียน PHP5 OOP (19)
- บทเรียน Joomla Framework พัฒนา Extension จุมล่า (33)
- บทเรียน Yii Framework (21)
- บทเรียน SQL (28)
- บทเรียน สอนการใช้งาน phpMyadmin (19)
- บทเรียน สอน PostgreSQL (23)
- บทเรียน Javascript (22)
- บทเรียน AJAX (10)
- บทเรียน jQuery (14)
- บทเรียน พัฒนา App บน Android OS (9)
- บทเรียน Python (58)
- บทเรียน Python GUI (145)
- บทเรียน Python Framework FastAPI (23)
- บทเรียน Python Tensorflow (14)
- บทเรียน Java (14)
- เรียนภาษาอังกฤษกับโปรแกรมเมอร์ ฉบับโปรแกรมเมอร์สอน (32)
- บทเรียนอื่นๆ (4)
- VDO Tutorial (0)
- บทเรียน Laravel Framework (6)
- บทเรียน Joomla 3 Framework พัฒนา Extension จุมล่า (16)
- บทเรียน Dart & Flutter (5)
- บทเรียน Joomla 4 Framework พัฒนา Extension จุมล่า Web Service (0)
กระทู้ล่าสุดจากเว็บบอร์ด