/
demo-captcha.php
83 lines (76 loc) · 1.8 KB
/
demo-captcha.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
<?php session_start(); ?>
<?xml version="1.0" encoding="utf-8"?>
<html>
<head>
<title>HTML_QuickForm2_Element_Captcha demo</title>
<style type="text/css">
span.error {
color: red;
}
div.element.error input {
background-color: #FAA;
}
</style>
</head>
<body>
<?php
set_include_path(
'../../'
. ':' . get_include_path()
);
require_once 'HTML/QuickForm2.php';
require_once 'HTML/QuickForm2/Element/NumeralCaptcha.php';
require_once 'HTML/QuickForm2/Element/ReCaptcha.php';
HTML_QuickForm2_Factory::registerElement(
'numeralcaptcha',
'HTML_QuickForm2_Element_NumeralCaptcha'
);
HTML_QuickForm2_Factory::registerElement(
'recaptcha',
'HTML_QuickForm2_Element_ReCaptcha'
);
$form = new HTML_QuickForm2(
'captchaform1', 'post',
array(),
false//change to true to use special ID in POST data
);
$form->addElement(
'numeralcaptcha', 'captchaelem',
array(
'id' => 'captchavalue',
)
);
$form->addElement(
'recaptcha', 'recaptchaelem',
array(
'id' => 'recaptchavalue',
),
array(
//Please get your own keys. This here is for demo purposes only.
'public-key' => '6LduXLoSAAAAAOH1LKWCyyqRsfE6SD6ZHOQg9kpr',
'private-key' => '6LduXLoSAAAAAH65fkp-xQHvekEBsNrt31SjBRZX'
)
);
$form->addElement(
'submit', 'submitted',
array('id' => 'submit', 'value' => 'Try it')
);
if ($form->validate()) {
echo '<h3>valid</h3>';
//clear the session, otherwise the user can re-submit the form
// again and again
foreach ($form->getElements() as $element) {
if ($element instanceof HTML_QuickForm2_Element_Captcha) {
$element->clearCaptchaSession();
}
}
} else {
echo '<h3>INvalid</h3>';
echo $form;
}
echo '<pre>Data: ';
var_dump($form->getValue());
echo '</pre>';
?>
</body>
</html>