ป้องกัน spam ด้วย captcha คำถาม ทางคณิตศาสตร์ อย่างง่าย

Bookmark and Share

PHP Code ตัวอย่าง
 

<?php
session_start();
?>
<!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>php question verify</title>
</head>
 
<body>
<?php
$_SESSION['num_to_check'][0]=rand(1,9);
$_SESSION['num_to_check'][1]=rand(1,9);
?>
<form id="form1" name="form1" method="post" action="">
  <div id="verify_math">
  <span class="digital" style="background-position:<?=($_SESSION['num_to_check'][0]*-30)?>px 0px;"></span>
  <span>+</span>
  <span class="digital" style="background-position:<?=($_SESSION['num_to_check'][1]*-30)?>px 0px;"></span>
  <span>=</span>
  <span>
  <input name="i_verify" type="text" id="i_verify" maxlength="2" />
</span>
</div>
<br />
<br />
<input type="submit" name="button2" id="button2" value="Submit" />
</form>
 
</body>
</html>

CSS จัดการ Captcha

<style type="text/css">
#verify_math{
	display:block;
	height:21px;	
}
#verify_math span{
	display:block;
	height:21px;	
	width:30px;
	position:relative;
	float:left;
	text-align:center;
	line-height:20px;
	color:#000;
}
#verify_math span.digital{
	background:url(images/digi_img.jpg) left top no-repeat;	
}
#i_verify{
	position:relative;
	height:15px;	
	width:35px;	
	text-align:center;
	padding:0;
	margin:0;
	font-size:15px;
	font-weight:bold;
	font-family:Tahoma, Geneva, sans-serif;
}
</style>

PHP Code หน้าตรวจสอบ
 

<?php
session_start();
if($_POST['button2']){
	if(isset($_POST['i_verify']) && $_POST['i_verify']==@array_sum($_SESSION['num_to_check']) && $_POST['i_verify']>0 && trim($_POST['i_verify'])!=""){
		echo "<span style="color:green;">Right</span>";
		$_SESSION['num_to_check'][0]=rand(1,9);
		$_SESSION['num_to_check'][1]=rand(1,9);
		exit;
	}else{
		echo "<span style="color:red;">Wrong</span>";
		$_SESSION['num_to_check'][0]=rand(1,9);
		$_SESSION['num_to_check'][1]=rand(1,9);		
		exit;	
	}
}
?>

ตัวอย่าง

http://www.ninenik.com/demo/php_question_verify.php

คลิกขวาเลือก save รูปนี้ไว้ใช้งาน
 




บทความในหมวดที่่น่าสนใจ อื่นๆ PHP Learning

30 Oct 09 ฟังก์ชัน PHP แปลงตัวเลขอาราบิก เป็น ตัวเลขไทย อย่างง่าย อ่าน 487 02 Mar 10 ประยุกต์ แปลเนื้อหา ในเว็บไซต์ด้วย google translater api สำหรับ php อ่าน 447 24 Aug 10 ประยุกต์ ใช้รายการ จากฐานข้อมูล สร้างปฏิทินกิจกรรม ด้วย fullcalendar และ jQuery อ่าน 429 17 Jun 10 สร้างฟังก์ชัน ตรวจสอบ การเข้าเว็บไซต์ ผ่านมือถือ ด้วย php อ่าน 416 23 Aug 10 Integrate ใช้ Filemanager ของ FCKeditor กับ CKEditor แทน CKFinder อ่าน 371 28 May 10 แสดง การแจ้งเตือน ให้เปิด หรือ บันทึกไฟล์ที่ดาวน์โหลด ด้วย php อ่าน 366 26 Mar 10 ความแตกต่าง ระหว่าง ฟังก์ชัน strstr กับ strrchr และ การประยุกต์ ใช้งาน อ่าน 359 03 Sep 10 จัดรูปแบบ url ลิ้งค์ link ด้วย เทคนิค php ได้อย่างง่าย อ่าน 253 31 Aug 10 เทคนิค php ใช้ fgets และ javascript วนลูป loop อ่านไฟล์ ขนาดใหญ่ อ่าน 220 06 Sep 10 ประยุกต์ การ invite friends ใน facebook มาใช้งาน อ่าน 183 07 Sep 10 ใช้ ckeditor กับ filemanager ด้วย php รองรับ ฟังก์ชัน javascript อ่าน 174 02 Sep 10 สร้าง ฟังก์ชัน ค้นหาโดเมน check domain ว่าง อย่างง่าย ด้วย php อ่าน 131
จำนวนผู้เยี่ยมชม 190989 คน 2010 © Copyright ninenik.com. All rights reserved.