Guestbook With a Database
You'll build a guestbook where visitors leave a line that's still there next week, stored in a database and shown back safely.
About
A counter in a file is memory. A guestbook is a database: many entries, each with a name, a message and a time, which you ask for in order and show back. SQLite is the one to use, because it's a single file, it comes with every language, and it runs a surprising share of the world's small apps. The skill under the badge is treating what visitors type as hostile, since a guestbook is the first place on your site where strangers write into your database and other strangers read it out. The database as a subject of its own, with schema design and indexes, lives in SQL & Databases. Here it's the thing behind the page.
At a glance
- Per requirement
- 25 XP
- Completion bonus
- 150 XP
- Requirements
- 6
- Takes
- One weekend
- Skill level
- Intermediate (months of practice)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- With other people
Requirements
-
1
Create a table with a column for each thing you'll store. One table called entries with id, name, message and created_at is enough. Write the CREATE TABLE statement in a file rather than clicking it together in a GUI, so you can run it again on a new machine. Take a screenshot of the statement and the empty table in the sqlite3 shell or DB Browser for SQLite. Photo
-
2
Insert entries with placeholders, never by pasting text into the query. The insert should read INSERT INTO entries (name, message) VALUES (?, ?) with the values passed separately. Building the query by joining strings is how one visitor with a quote mark in their name deletes your guestbook. Take a screenshot of the insert code with the placeholders visible. Photo
-
3
Show the entries newest first, escaped on the way out. Select the rows, loop over them in a template, and escape every value, either with a template engine that escapes by default or by hand. Then sign the guestbook with a script tag as your message and watch it appear as harmless text. Film the signing and the result. Video
-
4
Limit the length and the rate on the server. Cap the message at a few hundred characters in your code, since the browser's maxlength is a hint and not a rule, reject empty ones, and allow about one entry per address per minute. Without this the first bot to find your form fills the table in an hour. Take a screenshot of a rejected over-long or too-fast entry. Photo
-
5
Restart the server and check the entries are still there. Stop the process, start it again, and reload the page. Then open the database file in the sqlite3 shell and run a SELECT that returns the same rows the page shows. Take a screenshot of the shell and the page together. Photo
-
6
Get five people to sign it through a temporary tunnel. Open a free tunnel with a tool like ngrok or cloudflared so people outside your Wi-Fi can reach your laptop for an hour, send the link to five friends, and close the tunnel afterwards. One of them will type something you didn't expect, which is the point. Take a screenshot of the guestbook with five entries from people who aren't you. Photo