-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathmultiple_captchas_single_page.php
120 lines (101 loc) · 4.18 KB
/
multiple_captchas_single_page.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
<?php
/**
* Quick and dirty example of multiple captchas on one page.
*
* -- Synopsis --
* In order to add a captcha to multiple forms on a page, each image needs to
* have unique ID's for each <img> tag for the refresh button to work; they
* also need distinct names so the audio generation works properly.
*
* Since version 4.0 - namespaces are no longer used or required. Each captcha
* is now identified by a unique ID which is automatically generated when using
* getCaptchaHtml().
*
*/
// IMPORTANT!!!
// securimage.php must be included in order to use Securimage::getCaptchaHtml()
// Don't forget this!
require_once __DIR__ . '/../securimage.php';
?>
<!doctype html>
<title>Securimage :: Multiple captchas on one page</title>
<style>
body { text-align: center; padding: 150px; }
h3 { font-size: 20px; }
body { font: 20px Helvetica, sans-serif; color: #333; }
article { display: block; text-align: left; width: 650px; margin: 0 auto; }
a { color: #dc8100; text-decoration: none; }
a:hover { color: #333; text-decoration: none; }
label { display: block; }
em.valid { color: #00ccff; }
em.invalid { color: #f00; font-weight: bold; }
</style>
<article>
<h3>Multiple Captchas on one page using 'namespaces'</h3>
<fieldset>
<legend>"Contact Form" captcha...</legend>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>">
<input type="hidden" name="action" value="contact_form">
<?php
$error_html1 = null; // no error
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && $_POST['action'] == 'contact_form') {
// submitted "contact" form
// create new securimage object, indicating the namespace of the
// code we want to check based on what form was submitted.
$securimage = new Securimage();
// validate user input
$valid = $securimage->check(@$_POST['captcha_code'], $_POST['captcha_id']);
if ($valid) {
// code was correct, tell them so
$error_html1 = "<em class='valid'>Code entered correctly!</em><br>";
} else {
// incorrect code entered
$error_html1 = "<em class='invalid'>The code entered was incorrect.</em><br>";
}
}
// options controlling output of getCaptchaHtml()
$options1 = array(
'input_id' => 'contact_captcha', // ID of the text input field (must be unique on the page!)
'input_name' => 'captcha_code', // name of the captcha text field for POST
'image_id' => 'contact_captcha_img', // ID of the captcha image (must be unique on the page!)
'error_html' => $error_html1, // error (or success) to display to the user above text input
);
echo Securimage::getCaptchaHtml($options1);
?>
<br>
<input type="submit" value="Submit Form">
</form>
</fieldset>
<p>
Other page content here.....
</p>
<fieldset>
<legend>"Comments form" captcha...</legend>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>">
<input type="hidden" name="action" value="comment">
<?php
$error_html2 = null;
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && $_POST['action'] == 'comment') {
// submitted "contact" form
$securimage = new Securimage();
$valid = $securimage->check($_POST['captcha_code'], $_POST['captcha_id']);
if ($valid) {
$error_html2 = "<em class='valid'>Code entered correctly!</em><br>";
} else {
$error_html2 = "<em class='invalid'>The code entered was incorrect.</em><br>";
}
}
$options2 = array(
'input_id' => 'comments_captcha',
'input_name' => 'captcha_code',
'image_id' => 'comments_captcha_img',
'error_html' => $error_html2,
);
echo Securimage::getCaptchaHtml($options2);
?>
<br>
<input type="submit" value="Submit Form">
</form>
</fieldset>
</article>
</html>