This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.php
187 lines (166 loc) · 7.47 KB
/
index.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* Copyright (c) 2014 Adam L. Englander
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Mostly for the windows folks. Load the pdo and pdo_sqlite extensions if they are not loaded
foreach (array('pdo', 'pdo_sqlite') as $extension) {
if (!extension_loaded ($extension)) dl($extension);
}
// The default timezone should always be set when using dates and times
date_default_timezone_set('America/Los_Angeles');
// Turn off error reporting to hide it from the users. Hiding errors from users prevents exposing the internal
// workings of your application and the possibility of displaying secret or private data in the error.
error_reporting(0);
// Initialize our variables used in the template
$errors = array();
$first = null;
$last = null;
$result = null;
// Add a generic try catch around the entire PHP code section to ensure we catch any errors
try {
// Connect to database with PDO
$pdo = new PDO('sqlite:' . __DIR__ . '/registry.sq3');
// Set the
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($_POST) {
if (empty($_POST['first'])) {
// If the required field is empty add an error
$errors[] = 'First is required to register';
} else {
$first = $_POST['first'];
}
if (empty($_POST['last'])) {
// If the required field is empty add an error
$errors[] = 'Last is required to register';
} else {
$last = $_POST['last'];
}
$timestamp = time();
// If the form post had no errors then we will store the data in the database
if (!$errors) {
// Use try/catch to catch any errors in saving the registration
try {
// Use a prepared statement to protect against SQL injection
$stmt = $pdo->prepare('INSERT INTO registrants (first, last, registered_timestamp) VALUES (:first, :last, :timestamp)');
// Binding the data to the statement will escape the data before sending to the database in order to
// prevent SQL injection
$stmt->bindValue('first', $first);
$stmt->bindValue('last', $last);
$stmt->bindValue('timestamp', $timestamp, PDO::PARAM_INT);
$result = $stmt->execute();
// With a successful registration, clear the form values to allow for a new registration
$first = null;
$last = null;
} catch (PDOException $e) {
$message = sprintf(
'Unable to save registration: %s: Registrant info: [first: %s, last: %s, registered_timestamp: %s]',
$e->getMessage(),
$first,
$last,
$timestamp
);
// Throw new Exception with descriptive error message so the default handler will properly log an display the error
throw new Exception($message, 0, $e); // Set the caught exception as the previous
}
}
}
// Get the registrants
try {
$query = 'SELECT * FROM registrants ORDER BY registered_timestamp DESC';
$result = $pdo->query($query);
} catch (PDOException $e) {
$message = sprintf(
'Unable to retrieve registrants: %s',
$e->getMessage()
);
// Throw new Exception with descriptive error message so the default handler will properly log an display the error
throw new Exception(
$message,
0,
$e // Set the caught exception as the previous
);
}
} catch (Exception $e) {
// The default/master exception handler will log the error and display to the user
// Generate a Unique ID to identify this error
$errorId = uniqid('ERROR-');
// Add a nondescript error to the errors to show the user and include the error ID for reference
$errors[] = sprintf('An application error occurred [%s]', $errorId);
error_log(sprintf('%s: %s', $errorId, $e->getMessage()));
}
// All logic was handled above in the PHP section. This separates the logic from the view and makes the HTML
// section a bit more readable. It also allows for separating the controller from the view at a later time.
?>
<!DOCTYPE html>
<html>
<body>
<div>
<h1>Registration Application</h1>
<p>This is a basic registration application</p>
</div>
<div>
<h2>Register</h2>
<?php if ($errors): // If there are errors, display them to the user ?>
<div class="errors">
<h3 class="error-heading">Errors were encountered wth your registration</h3>
<ul>
<?php foreach ($errors as $error): // Loop through the errors array ?>
<li><?php echo $error; // Display an individual error ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<form method="POST">
<div>
<label for="first">First: </label>
<input id="first" name="first" value="<?php echo htmlentities($first); // Always protect against HTML injection with htmlentities() ?>">
<label for="last">Last: </label>
<input id="last" name="last" value="<?php echo htmlentities($last); // Always protect against HTML injection with htmlentities() ?>">
<input type="submit" value="Register">
</div>
</form>
</div>
<div class="registration-list">
<h2>Registrations</h2>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Date</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<?php if ($result): // Check to make shre you have a valid result object before iterating over it ?>
<?php while ($registrant = $result->fetch()): // Fetch the next record of the result ?>
<tr>
<td><?php echo htmlentities($registrant['first']); // Always protect against HTML injection with htmlentities() ?>
<td><?php echo htmlentities($registrant['last']); // Always protect against HTML injection with htmlentities() ?>
<td><?php echo date('M j, Y', $registrant['registered_timestamp']); // Format the timestamp as a date ?>
<td><?php echo date('g:i:s A', $registrant['registered_timestamp']); // Format the timestamp as a time ?>
</tr>
<?php endwhile; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</body>
</html>