-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclick_captcha_demo.php
More file actions
168 lines (150 loc) · 4.82 KB
/
click_captcha_demo.php
File metadata and controls
168 lines (150 loc) · 4.82 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* 测试session storage captcha
*
* 创建图片
* click_captcha_demo.php?action=create&storage_type=session
*
* 验证
* click_captcha_demo.php?action=validate&storage_type=session&validate_code=xxx
*
*
* 测试redis storage captcha
*
* 创建图片
* click_captcha_demo.php?action=create&storage_type=redis
*
* 验证
* click_captcha_demo.php?action=validate&storage_type=redis&validate_code=xxx
*/
require 'autoload.php';
/**
* 测试ClickCaptcha生成与验证
*/
class TestClickCaptcha{
/**
* 操作类型
* create/validate
*
* @var string
*/
private $action;
/**
* 使用的存储类型
* session/redis
*
* @var string
*/
private $storage_type;
/**
* 初始化
*
* @param string $action 操作类型
* @param string $storage_type 使用的存储类型
*/
public function __construct(string $action, string $storage_type){
if(!in_array($action, ['create', 'validate'])){
throw new \Exception('action not exists');
}
if(!in_array(strtolower($storage_type), [strtolower(\Captcha\Storage\Type::SESSION), strtolower(\Captcha\Storage\Type::REDIS)])){
throw new \Exception('storage type not exists');
}
$this->action = $action;
$this->storage_type = $storage_type;
}
/**
* 执行测试
*
* @param string $key 唯一标识
* @return void
*/
public function run(string $key):void{
// storage
if(strtolower($this->storage_type)==strtolower(\Captcha\Storage\Type::SESSION)){
$captcha_storage = $this->createSessionStorage();
}elseif(strtolower($this->storage_type)==strtolower(\Captcha\Storage\Type::REDIS)){
$captcha_storage = $this->createRedisStorage();
}
// action
if($this->action=='create'){
$this->create($key, $captcha_storage);
}elseif($this->action=='validate'){
// 获取用户输入的验证码
$validate_code = isset($_GET['validate_code'])? $_GET['validate_code'] : '';
$this->validate($key, $validate_code, $captcha_storage);
}
}
/**
* 创建session存储对象
*
* @return \Captcha\Storage\IStorage
*/
private function createSessionStorage():\Captcha\Storage\IStorage{
// session storage
session_start();
$session_config = array(
'expire' => 60
);
$session_storage_config = new \Captcha\Storage\SessionStorageConfig($session_config);
$session_storage = \Captcha\Storage\Factory::make(\Captcha\Storage\Type::SESSION, $session_storage_config);
return $session_storage;
}
/**
* 创建redis存储对象
*
* @return \Captcha\Storage\IStorage
*/
private function createRedisStorage():\Captcha\Storage\IStorage{
// redis storage
$redis_config = array(
'connect_config' => array(
'host' => 'redis',
'port' => 6379,
'index' => 0,
'auth' => '',
'timeout' => 1,
'reserved' => NULL,
'retry_interval' => 100,
),
'expire' => 60
);
$redis_storage_config = new \Captcha\Storage\RedisStorageConfig($redis_config);
$redis_storage = \Captcha\Storage\Factory::make(\Captcha\Storage\Type::REDIS, $redis_storage_config);
return $redis_storage;
}
/**
* 创建验证码图片
*
* @param string $key 唯一标识
* @param \Captcha\Storage\IStorage $storage 存储对象
* @return void
*/
private function create(string $key, \Captcha\Storage\IStorage $storage):void{
$captcha_config = new \Captcha\ClickCaptchaConfig($key, $storage);
$captcha_config->setWidth(250);
$captcha_config->setHeight(150);
$captcha_config->setGraphNum(5);
$captcha_config->setGraphSize(45);
// 输出验证码图片
\Captcha\ClickCaptcha::create($key, $captcha_config);
}
/**
* 执行验证
*
* @param string $key 唯一标识
* @param string $validate_code 用户输入的验证码
* @param \Captcha\Storage\IStorage $storage 存储对象
* @return void
*/
private function validate(string $key, string $validate_code, \Captcha\Storage\IStorage $storage):void{
// 执行验证
$ret = \Captcha\ClickCaptcha::validate($key, $validate_code, $storage);
var_dump($ret);
}
}
// 获取操作与使用的存储类型
$action = isset($_GET['action'])? $_GET['action'] : 'create';
$storage_type = isset($_GET['storage_type'])? $_GET['storage_type'] : \Captcha\Storage\Type::SESSION;
$key = 'uuid_key';
$captcha_test = new TestClickCaptcha($action, $storage_type);
$captcha_test->run($key);