激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - php實現的click captcha點擊驗證碼類實例

php實現的click captcha點擊驗證碼類實例

2020-07-29 15:36shichen2014 PHP教程

這篇文章主要介紹了php實現的click captcha點擊驗證碼類實例,不同于以往傳統的驗證碼,該驗證碼類可實現手機用戶點擊某一位置確認驗證碼,非常實用,需要的朋友可以參考下

本文實例講述了php實現的click captcha點擊驗證碼及其用法,是非常實用的功能。分享給大家供大家參考之用。具體如下:

一、需求:

現在常用的表單驗證碼大部分都是要用戶輸入為主,但這樣對手機用戶會不方便。
如果手機用戶訪問,可以不用輸入,而是click某一位置便可確認驗證碼,這樣就會方便很多。

二、原理:

1.使用PHP imagecreate創建PNG圖象,在圖中畫N個圓弧,其中一個是完整的圓(驗證用),將圓心坐標及半徑記錄入session。

2.在瀏覽器,當用戶在驗證碼圖片上點擊時,記錄點擊的位置。

3.將用戶點擊的坐標與session記錄的圓心坐標、半徑比較,判斷是否在圓中,如是則驗證通過。

程序運行效果如下圖所示:

php實現的click captcha點擊驗證碼類實例

三、實現方法:

ClickCaptcha.class.php類文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?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示例程序如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人午夜视屏 | 12av毛片| 亚洲成年人免费网站 | 国产精品久久久久久久av三级 | 92精品国产自产在线 | 国产精品自在线拍 | 黄色av网站在线观看 | 国产精选电影免费在线观看 | 性生活视频软件 | 中国毛片在线观看 | 爱唯侦察 国产合集 亚洲 | 免费国产成人高清在线看软件 | 羞羞视频免费网站含羞草 | 久久美女免费视频 | 成人午夜激情网 | 国产一级91| 国内精品免费一区二区2001 | 久久精品无码一区二区三区 | 国产精品一区二区日韩 | 国产91小视频在线观看 | 羞羞视频免费网站入口 | 亚洲午夜一区二区三区 | 性看小视频 | 亚洲成人黄色片 | 国产69精品久久久久9999不卡免费 | 久久精品男人 | 久久精品电影网 | 91重口视频 | 97青青| 看免费黄色大片 | a级高清免费毛片av在线 | 免费一级特黄欧美大片勹久久网 | 欧美成在线视频 | 日本娇小videos高潮 | 成人激情综合网 | 国产精品jk白丝蜜臀av软件 | 久久综合婷婷香五月 | 日本中文一级片 | 久久久久久久黄色片 | 亚洲免费观看视频 | 在线小视频国产 |