-
Notifications
You must be signed in to change notification settings - Fork 0
/
captcha_test.php.bu
137 lines (119 loc) · 3.24 KB
/
captcha_test.php.bu
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
<?php
session_start();
$_SESSION = array();
include("captcha.php");
$_SESSION['captcha'] = simple_php_captcha();
?>
<!DOCTYPE html>
<html>
<head>
<title>Example » A simple PHP CAPTCHA script</title>
<style type="text/css">
pre {
border: solid 1px #bbb;
padding: 10px;
margin: 2em;
}
img {
border: solid 1px #ccc;
margin: 0 2em;
}
</style>
</head>
<body>
<h1>
CAPTCHA Example
</h1>
<h2>Usage</h2>
<p>
The following code will prepare a CAPTCHA image and keep the code in a session
variable for later use:
</p>
<pre>
<?php
session_start();
include("captcha.php");
$_SESSION['captcha'] = simple_php_captcha();
?>
</pre>
<p>
After the call to <code>simple_php_captcha()</code> above,
<code>$_SESSION['captcha']</code> will be something like this:
</p>
<pre>
<?php
print_r($_SESSION['captcha']);
?>
</pre>
<p>
To display the CAPTCHA image, create an HTML <code><img></code> using
<code>$_SESSION['captcha']['image_src']</code> as the <code>src</code> attribute:
</p>
<p>
<?php
echo '<img src="' . $_SESSION['captcha']['image_src'] . '" alt="CAPTCHA code">';
?>
</p>
<p>
To verify the CAPTCHA value on the next page load (or in an AJAX request), test
against <code>$_SESSION['captcha']['code']</code>. You can use
<code>strtolower()</code> or <code>strtoupper()</code> to perform a
case-insensitive match.
</p>
<h2>Configuration</h2>
<p>
Configuration is easy and all values are optional. To specify one or more options,
do this:
</p>
<pre>
<?php
$_SESSION['captcha'] = simple_php_captcha( array(
'min_length' => 5,
'max_length' => 5,
'backgrounds' => array(image.png', ...),
'fonts' => array('font.ttf', ...),
'characters' => 'ABCDEFGHJKLMNPRSTUVWXYZabcdefghjkmnprstuvwxyz23456789',
'min_font_size' => 28,
'max_font_size' => 28,
'color' => '#666',
'angle_min' => 0,
'angle_max' => 10,
'shadow' => true,
'shadow_color' => '#fff',
'shadow_offset_x' => -1,
'shadow_offset_y' => 1
));
>
</pre>
<h2>Notes</h2>
<ul>
<li>
<strong>Important!</strong> Make sure you call <code>session_start()</code> before
calling the <code>simple_php_captcha()</code> function
</li>
<li>
Requires PHP GD2 library
</li>
<li>
Backgound images must be in PNG format
</li>
<li>
Fonts must be either TTF or OTF
</li>
<li>
Backgrounds and fonts must be specified using their full paths (tip: use
<code>$_SERVER['DOCUMENT_ROOT'] . '/' . [path-to-file]</code>)
</li>
<li>
Angles should not exceed approximately 15 degrees, as the text will sometimes
appear outside of the viewable area
</li>
<li>
Creates a function called <code>simple_php_captcha()</code> in the global namespace
</li>
<li>
Uses the <code>$_SESSION['simple-php-captcha']</code> session variable
</li>
</ul>
</body>
</html>