ด้วยสํานึกในพระมหากรุณาธิคุณสมเด็จพระนางเจ้าสิริกิติ์เป็นล้นพ้นอันหาที่สุดมิได้
ด้วยสํานึกในพระมหากรุณาธิคุณสมเด็จพระนางเจ้าสิริกิติ์เป็นล้นพ้นอันหาที่สุดมิได้


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

เขียนเมื่อ 15 ปีก่อน โดย Ninenik Narkdee
captcha ป้องกัน spam

คำสั่ง การ กำหนด รูปแบบ ตัวอย่าง เทคนิค ลูกเล่น การประยุกต์ การใช้งาน เกี่ยวกับ captcha ป้องกัน spam

ปัจจุบัน นักพัฒนาสามารถ ใช้ ChatGPT | Gemini | Claude | Perplexity | Deepseek ช่วยในการแก้ไขปัญหาต่างๆ ในการเขียนโปรแกรม หรือหาข้อมูลเพิ่มเติมได้ง่ายและสะดวก แนะนำให้ทุกคนใช้งานเพื่อพัฒนาศักยภาพของตัวเอง

ดูแล้ว 14,387 ครั้ง


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;	
	}
}
?>

ตัวอย่าง

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

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










เนื้อหาที่เกี่ยวข้อง









URL สำหรับอ้างอิง








เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ