Created
January 18, 2024 23:55
-
-
Save remysucre/90ebbba077986852584c3009de4d8478 to your computer and use it in GitHub Desktop.
Simple CGI script for a guestbook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/ksh | |
# Function to create the SQLite database and table if they don't exist | |
create_database() { | |
if [ ! -f guestbook.db ]; then | |
sqlite3 guestbook.db 'CREATE TABLE entries (id INTEGER PRIMARY KEY AUTOINCREMENT, message TEXT)' | |
fi | |
} | |
# Function to fetch and display guestbook entries | |
display_guestbook_entries() { | |
entries=$(sqlite3 guestbook.db 'SELECT message FROM entries ORDER BY id DESC' | sed 's/message=/<li>/g') | |
echo "<h2>Guestbook Entries:</h2>" | |
echo "<ul>" | |
echo "$entries" | |
echo "</ul>" | |
} | |
# Content-type header | |
echo "Content-type: text/html" | |
echo "" | |
# HTML content | |
cat << EOF | |
<html> | |
<head> | |
<title>Guestbook</title> | |
</head> | |
<body> | |
<h1>Guestbook</h1> | |
<form method='post' action='guestbook.cgi' enctype='text/plain'> | |
<label for='message'>Leave a message:</label> | |
<input type='text' name='message' id='message' required> | |
<input type='submit' value='Submit'> | |
</form> | |
EOF | |
# Create the database and table if they don't exist | |
create_database | |
# Handle form submission | |
if [ "$REQUEST_METHOD" = "POST" ]; then | |
read -r message | |
sqlite3 guestbook.db "INSERT INTO entries (message) VALUES ('$message')" | |
fi | |
display_guestbook_entries | |
echo "</body>" | |
echo "</html>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment