-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalcaptcha.php
More file actions
executable file
·85 lines (80 loc) · 2.63 KB
/
localcaptcha.php
File metadata and controls
executable file
·85 lines (80 loc) · 2.63 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
<?php
/**
* Created by PhpStorm.
* User: Jerry
* Date: 2016/11/18
* Time: 上午1:06
*/
$char = array(0 => "0", 1 => '1', 2 => '2', 3 => '3', 4 => '4',
5 => '5', 6 => '6', 7 => '7', 8 => '8', 9 => '9',
'+' => '加', '-' => '减', '*' => '乘', '=' => '=');
session_start();
$_SESSION['isPass'] = 0;
if (isset($_POST['get_captcha'])) {
if ($_POST['get_captcha'] == 1) {
$method = rand(1, 3);//1 -> +, 2 -> -, 3 -> *
$_SESSION['setCapt'] = 1;
if ($method == 1) {
$a = rand(0, 9);
$b = rand(0, 9);
$c = rand(0, 9);
$d = rand(0, 9);
$x = $a * 10 + $b;
$y = $c * 10 + $d;
$_SESSION['answer'] = $x + $y;
$ret = $char[$a] . $char[$b] . $char['+'] . $char[$c] . $char[$d] . $char['='];
echo json_encode(array('captcha' => $ret));
} else if ($method == 2) {
$a = rand(0, 9);
$b = rand(0, 9);
$c = rand(0, 9);
$d = rand(0, 9);
$x = $a * 10 + $b;
$y = $c * 10 + $d;
$_SESSION['answer'] = $x - $y;
$ret = $char[$a] . $char[$b] . $char['-'] . $char[$c] . $char[$d] . $char['='];
echo json_encode(array('captcha' => $ret));
} else if ($method == 3) {
$a = rand(0, 1);
$b = rand(0, 9);
$c = rand(0, 1);
$d = rand(0, 9);
$x = $a * 10 + $b;
$y = $c * 10 + $d;
$_SESSION['answer'] = $x * $y;
$ret = $char[$a] . $char[$b] . $char['*'] . $char[$c] . $char[$d] . $char['='];
echo json_encode(array('captcha' => $ret));
}
}
}
if (isset($_POST['test_captcha'])) {
if (isset($_SESSION['setCapt'])) {
if ($_SESSION['setCapt'] == 1) {
if ($_POST['test_captcha'] == $_SESSION['answer']) {
$_SESSION['isPass'] = 1;
$ret = json_encode(array("status" => true));
echo $ret;
} else {
$ret = json_encode(array("status" => false));
echo $ret;
}
} else {
$ret = json_encode(array("status" => false));
echo $ret;
}
} else {
$ret = json_encode(array("status" => false));
echo $ret;
}
unset($_SESSION['answer']);
}
//测试验证码是否正常运行,部署时应删除
/*
if (isset($_POST['status'])) {
if ($_POST['status'] == 1) {
$ret = json_encode(array('setCapt' => $_SESSION['setCapt'], 'answer' => $_SESSION['answer'],
'isPass' => $_SESSION['isPass']));
echo $ret;
}
}
*/