本文实例讲述了php实现的click captcha点击验证码类及其用法,是非常实用的功能。分享给大家供大家参考之用。具体如下:
一、需求:
现在常用的表单验证码大部分都是要用户输入为主,但这样对手机用户会不方便。
如果手机用户访问,可以不用输入,而是click某一位置便可确认验证码,这样就会方便很多。
二、原理:
1.使用php imagecreate创建png图象,在图中画n个圆弧,其中一个是完整的圆(验证用),将圆心坐标及半径记录入session。
2.在浏览器,当用户在验证码图片上点击时,记录点击的位置。
3.将用户点击的坐标与session记录的圆心坐标、半径比较,判断是否在圆中,如是则验证通过。
程序运行效果如下图所示:
三、实现方法:
clickcaptcha.class.php类文件如下:
<?php
/** click captcha 验证码类
* date: 2013-05-04
* author: fdipzone
* ver: 1.0
*/
class clickcaptcha { // class start
public $sess_name = 'm_captcha';
public $width = 500;
public $height = 200;
public $icon = 5;
public $iconcolor = array(255, 255, 0);
public $backgroundcolor = array(0, 0, 0);
public $iconsize = 56;
private $_img_res = null;
public function __construct($sess_name=''){
if(session_id() == ''){
session_start();
}
if($sess_name!=''){
$this->sess_name = $sess_name; // 设置session name
}
}
/** 创建验证码 */
public function create(){
// 创建图象
$this->_img_res = imagecreate($this->width, $this->height);
// 填充背景
imagecolorallocate($this->_img_res, $this->backgroundcolor[0], $this->backgroundcolor[1], $this->backgroundcolor[2]);
// 分配颜色
$col_ellipse = imagecolorallocate($this->_img_res, $this->iconcolor[0], $this->iconcolor[1], $this->iconcolor[2]);
$minarea = $this->iconsize/2+3;
// 混淆用图象,不完整的圆
for($i=0; $i<$this->icon; $i++){
$x = mt_rand($minarea, $this->width-$minarea);
$y = mt_rand($minarea, $this->height-$minarea);
$s = mt_rand(0, 360);
$e = $s + 330;
imagearc($this->_img_res, $x, $y, $this->iconsize, $this->iconsize, $s, $e, $col_ellipse);
}
// 验证用图象,完整的圆
$x = mt_rand($minarea, $this->width-$minarea);
$y = mt_rand($minarea, $this->height-$minarea);
$r = $this->iconsize/2;
imagearc($this->_img_res, $x, $y, $this->iconsize, $this->iconsize, 0, 360, $col_ellipse);
// 记录圆心坐标及半径
$this->captcha_session($this->sess_name, array($x, $y, $r));
// 生成图象
header("content-type: image/png");
imagepng($this->_img_res);
imagedestroy($this->_img_res);
exit();
}
/** 检查验证码
* @param string $captcha 验证码
* @param int $flag 验证成功后 0:不清除session 1:清除session
* @return boolean
*/
public function check($captcha, $flag=1){
if(trim($captcha)==''){
return false;
}
if(!is_array($this->captcha_session($this->sess_name))){
return false;
}
list($px, $py) = explode(',', $captcha);
list($cx, $cy, $cr) = $this->captcha_session($this->sess_name);
if(isset($px) && is_numeric($px) && isset($py) && is_numeric($py) &&
isset($cx) && is_numeric($cx) && isset($cy) && is_numeric($cy) && isset($cr) && is_numeric($cr)){
if($this->pointinarea($px,$py,$cx,$cy,$cr)){
if($flag==1){
$this->captcha_session($this->sess_name,'');
}
return true;
}
}
return false;
}
/** 判断点是否在圆中
* @param int $px 点x
* @param int $py 点y
* @param int $cx 圆心x
* @param int $cy 圆心y
* @param int $cr 圆半径
* sqrt(x^2+y^2)<r
*/
private function pointinarea($px, $py, $cx, $cy, $cr){
$x = $cx-$px;
$y = $cy-$py;
return round(sqrt($x*$x + $y*$y))<$cr;
}
/** 验证码session处理方法
* @param string $name captcha session name
* @param string $value
* @return string
*/
private function captcha_session($name,$value=null){
if(isset($value)){
if($value!==''){
$_session[$name] = $value;
}else{
unset($_session[$name]);
}
}else{
return isset($_session[$name])? $_session[$name] : '';
}
}
} // class end
?>
demo.php示例程序如下:
<?php
session_start();
require('clickcaptcha.class.php');
if(isset($_get['get_captcha'])){ // get captcha
$obj = new clickcaptcha();
$obj->create();
exit();
}
if(isset($_post['send']) && $_post['send']=='true'){ // submit
$name = isset($_post['name'])? trim($_post['name']) : '';
$captcha = isset($_post['captcha'])? trim($_post['captcha']) : '';
$obj = new clickcaptcha();
if($obj->check($captcha)){
echo 'your name is:'.$name;
}else{
echo 'captcha not match';
}
echo ' <a href="demo.php">back</a>';
}else{ // html
?>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title> click captcha demo </title>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(function(){
$('#captcha_img').click(function(e){
var x = e.pagex - $(this).offset().left;
var y = e.pagey - $(this).offset().top;
$('#captcha').val(x+','+y);
})
$('#btn').click(function(e){
if($.trim($('#name').val())==''){
alert('please input name!');
return false;
}
if($.trim($('#captcha').val())==''){
alert('please click captcha!');
return false;
}
$('#form1')[0].submit();
})
})
</script>
</head>
<body>
<form name="form1" id="form1" method="post" action="demo.php" onsubmit="return false">
<p>name:<input type="text" name="name" id="name"></p>
<p>captcha:please click full circle<br><img id="captcha_img" src="demo.php?get_captcha=1&t=<?=time() ?>" style="cursor:pointer"></p>
<p><input type="submit" id="btn" value="submit"></p>
<input type="hidden" name="send" value="true">
<input type="hidden" name="captcha" id="captcha">
</form>
</body>
</html>
<?php } ?>
本文完整源码点击此处本站下载。
希望本文所述对大家的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!