小标
2018-07-12
来源 :
阅读 3239
评论 0
摘要:本文主要向大家介绍了PHP语言 click captcha 验证码类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
本文主要向大家介绍了PHP语言 click captcha 验证码类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
需求:
现在常用的表单验证码大部分都是要用户输入为主,但这样对手机用户会不方便。
如果手机用户访问,可以不用输入,而是click某一位置便可确认验证码,这样就会方便很多。
原理:
1.使用PHP imagecreate创建PNG图象,在图中画N个圆弧,其中一个是完整的圆(验证用),将圆心坐标及半径记录入session。
2.在浏览器,当用户在验证码图片上点击时,记录点击的位置。
3.将用户点击的坐标与session记录的圆心坐标、半径比较,判断是否在圆中,如是则验证通过。
ClickCaptcha.class.php
[php] view plain copy
1. <?php
2. /** Click Captcha 验证码类
3. * Date: 2013-05-04
4. * Author: fdipzone
5. * Ver: 1.0
6. */
7.
8. class ClickCaptcha { // class start
9.
10. public $sess_name = 'm_captcha';
11. public $width = 500;
12. public $height = 200;
13. public $icon = 5;
14. public $iconColor = array(255, 255, 0);
15. public $backgroundColor = array(0, 0, 0);
16. public $iconSize = 56;
17.
18. private $_img_res = null;
19.
20.
21. public function __construct($sess_name=''){
22. if(session_id() == ''){
23. session_start();
24. }
25.
26. if($sess_name!=''){
27. $this->sess_name = $sess_name; // 设置session name
28. }
29. }
30.
31.
32. /** 创建验证码 */
33. public function create(){
34.
35. // 创建图象
36. $this->_img_res = imagecreate($this->width, $this->height);
37.
38. // 填充背景
39. ImageColorAllocate($this->_img_res, $this->backgroundColor[0], $this->backgroundColor[1], $this->backgroundColor[2]);
40.
41. // 分配颜色
42. $col_ellipse = imagecolorallocate($this->_img_res, $this->iconColor[0], $this->iconColor[1], $this->iconColor[2]);
43.
44. $minArea = $this->iconSize/2+3;
45.
46. // 混淆用图象,不完整的圆
47. for($i=0; $i<$this->icon; $i++){
48. $x = mt_rand($minArea, $this->width-$minArea);
49. $y = mt_rand($minArea, $this->height-$minArea);
50. $s = mt_rand(0, 360);
51. $e = $s + 330;
52. imagearc($this->_img_res, $x, $y, $this->iconSize, $this->iconSize, $s, $e, $col_ellipse);
53. }
54.
55. // 验证用图象,完整的圆
56. $x = mt_rand($minArea, $this->width-$minArea);
57. $y = mt_rand($minArea, $this->height-$minArea);
58. $r = $this->iconSize/2;
59. imagearc($this->_img_res, $x, $y, $this->iconSize, $this->iconSize, 0, 360, $col_ellipse);
60.
61. // 记录圆心坐标及半径
62. $this->captcha_session($this->sess_name, array($x, $y, $r));
63.
64. // 生成图象
65. Header("Content-type: image/PNG");
66. ImagePNG($this->_img_res);
67. ImageDestroy($this->_img_res);
68.
69. exit();
70. }
71.
72.
73. /** 检查验证码
74. * @param String $captcha 验证码
75. * @param int $flag 验证成功后 0:不清除session 1:清除session
76. * @return boolean
77. */
78. public function check($captcha, $flag=1){
79. if(trim($captcha)==''){
80. return false;
81. }
82.
83. if(!is_array($this->captcha_session($this->sess_name))){
84. return false;
85. }
86.
87. list($px, $py) = explode(',', $captcha);
88. list($cx, $cy, $cr) = $this->captcha_session($this->sess_name);
89.
90. if(isset($px) && is_numeric($px) && isset($py) && is_numeric($py) &&
91. isset($cx) && is_numeric($cx) && isset($cy) && is_numeric($cy) && isset($cr) && is_numeric($cr)){
92. if($this->pointInArea($px,$py,$cx,$cy,$cr)){
93. if($flag==1){
94. $this->captcha_session($this->sess_name,'');
95. }
96. return true;
97. }
98. }
99. return false;
100. }
101.
102.
103. /** 判断点是否在圆中
104. * @param int $px 点x
105. * @param int $py 点y
106. * @param int $cx 圆心x
107. * @param int $cy 圆心y
108. * @param int $cr 圆半径
109. * sqrt(x^2+y^2)<r
110. */
111. private function pointInArea($px, $py, $cx, $cy, $cr){
112. $x = $cx-$px;
113. $y = $cy-$py;
114. return round(sqrt($x*$x + $y*$y))<$cr;
115. }
116.
117.
118. /** 验证码session处理方法
119. * @param String $name captcha session name
120. * @param String $value
121. * @return String
122. */
123. private function captcha_session($name,$value=null){
124. if(isset($value)){
125. if($value!==''){
126. $_SESSION[$name] = $value;
127. }else{
128. unset($_SESSION[$name]);
129. }
130. }else{
131. return isset($_SESSION[$name])? $_SESSION[$name] : '';
132. }
133. }
134.
135. } // class end
136.
137. ?>
demo.php
[php] view plain copy
1. <?php
2. session_start();
3. require('ClickCaptcha.class.php');
4.
5. if(isset($_GET['get_captcha'])){ // get captcha
6. $obj = new ClickCaptcha();
7. $obj->create();
8. exit();
9. }
10.
11. if(isset($_POST['send']) && $_POST['send']=='true'){ // submit
12. $name = isset($_POST['name'])? trim($_POST['name']) : '';
13. $captcha = isset($_POST['captcha'])? trim($_POST['captcha']) : '';
14.
15. $obj = new ClickCaptcha();
16.
17. if($obj->check($captcha)){
18. echo 'your name is:'.$name;
19. }else{
20. echo 'captcha not match';
21. }
22. echo ' <a href="demo.php">back</a>';
23.
24. }else{ // html
25. ?>
26. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "//www.w3.org/TR/html4/loose.dtd">
27. <html>
28. <head>
29. <meta http-equiv="content-type" content="text/html; charset=utf-8">
30. <title> Click Captcha Demo </title>
31. <script type="text/javascript" src="jquery-1.6.2.min.js"></script>
32. <script type="text/javascript">
33. $(function(){
34. $('#captcha_img').click(function(e){
35. var x = e.pageX - $(this).offset().left;
36. var y = e.pageY - $(this).offset().top;
37. $('#captcha').val(x+','+y);
38. })
39.
40. $('#btn').click(function(e){
41. if($.trim($('#name').val())==''){
42. alert('Please input name!');
43. return false;
44. }
45.
46. if($.trim($('#captcha').val())==''){
47. alert('Please click captcha!');
48. return false;
49. }
50. $('#form1')[0].submit();
51. })
52. })
53. </script>
54. </head>
55.
56. <body>
57. <form name="form1" id="form1" method="post" action="demo.php" onsubmit="return false">
58. <p>name:<input type="text" name="name" id="name"></p>
59. <p>Captcha:Please click full circle<br><img id="captcha_img" src="demo.php?get_captcha=1&t=<?=time() ?>" style="cursor:pointer"></p>
60. <p><input type="submit" id="btn" value="submit"></p>
61. <input type="hidden" name="send" value="true">
62. <input type="hidden" name="captcha" id="captcha">
63. </form>
64. </body>
65. </html>
66. <?php } ?>
本文由职坐标整理并发布,了解更多内容,请关注职坐标编程语言PHP频道!
喜欢 | 1
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号