PowerBASIC Forums
  Source Code
  Guestbook in PB/CC 1.0

Post New Topic  Post A Reply
profile | register | preferences | faq | search

UBBFriend: Email This Page to Someone! next newest topic | next oldest topic
Author Topic:   Guestbook in PB/CC 1.0
Bud Durland
Member
posted July 04, 2000 11:13 AM     Click Here to See the Profile for Bud Durland     Edit/Delete Message   Reply w/Quote
I needed a guestbook for our web site at work, and this
is the result. It has two parts, which I'll post as separate
replies to this message.

The first part is SIGNBK. It takes guestbook input from a web
page form & saves it in a file. It also logs the date of the
entry, and the IP address of the person who made it.

The second part is GUESTBK. This program reads the file and
formats it for display in the browser. Only a certain number of
guestbook entries are displayed at a time (the default is 10).
GUESTBK generates links for going forward for backward in the
guestbook file. It also creates and displays 'mailto' links,
if the user has entered their e-mail address.

I wrote this for a relatively low-traffic site; therefore,
there's little or no concern in the code for simultaneous
access of the guestbook data file by more than one visitor.
YMMV.

Generally, you'll enter the guestbook with a link to
"/scripts/guestbk.exe" or something similar; the GUESTBK program
creates a link to the page where folks sign the guestbook.

Hope somebody finds it useful.

------------------

[This message has been edited by Bud Durland (edited July 04, 2000).]

IP: Logged

Bud Durland
Member
posted July 04, 2000 11:14 AM     Click Here to See the Profile for Bud Durland     Edit/Delete Message   Reply w/Quote
This is the code for SIGNBK.BAS :


GLOBAL ReturnURL AS STRING
TYPE SYSTEMTIME
wYear AS INTEGER
wMonth AS INTEGER
wDayOfWeek AS INTEGER
wDay AS INTEGER
wHour AS INTEGER
wMinute AS INTEGER
wSecond AS INTEGER
wMilliseconds AS INTEGER
END TYPE

DECLARE SUB HTMLHeader()
DECLARE SUB HTMLFooter()
DECLARE SUB GetLocalTime LIB "KERNEL32.DLL" ALIAS "GetLocalTime" (lpSystemTime AS SYSTEMTIME)
DECLARE FUNCTION GetDateFormat LIB "KERNEL32.DLL" ALIAS "GetDateFormatA" (BYVAL Locale AS LONG, BYVAL dwFlags AS LONG, lpDate AS SYSTEMTIME, lpFormat AS ASCIIZ, lpDateStr AS ASCIIZ, BYVAL cchDate AS LONG) AS LONG
DECLARE FUNCTION GetTimeFormat LIB "KERNEL32.DLL" ALIAS "GetTimeFormatA" (BYVAL Locale AS LONG, BYVAL dwFlags AS LONG, lpTime AS SYSTEMTIME, lpFormat AS ASCIIZ, lpTimeStr AS ASCIIZ, BYVAL cchTime AS LONG) AS LONG

$INCLUDE "PBCGI.INC"

FUNCTION PBMAIN
'+------------------------------------------------------------------------
' program to add entries to a Guestbook file
'
' What this program does:
' Accept Guest book input from a submitted webform.
' Add it to the existing guest book entries
' And, that's about it.
'
' SIGNBK's sister script (GUESTBK) actually reads and formats the data
'
' You have to devise an HTML page with a form to gather the guestbook
' data. There are three required fields for the form, and one optional
' field. The names of the fileds should explain what goes in them:
'
' Required field THENAME == Guest's name
' Required field COMMENTS == Guest's comments for the guest book
' Optional field EMAIL == Guest's e-mail address
' Optional field RETURNURL == Page to take user to after data is saved
'
' Normally, you won't specify a RETURNURL field. This program will
' default the field to "/scripts/guestbk.exe", so that guests will be
' able to see their entry immediately. However, if you do want to
' specifiy this field in the form, you should use a hidden field
'
' If the user enters their e-mail address, their name will be displayed
' as a clickable e-mail link in the guestbook
'
' When the data is saved, the entire guestbook file is re-written so
' that the newest comments are at the beginning. With very lengthy
' guest books, this could become a problem; by then I'll come up
' with a different way of doing it
'
' This progam doesn't do any file locking, or do much checking for
' simultaneous file access. I wrote it for a relatively low-traffic site
' and don't expect any trouble. ymmv.
'+-------------------------------------------------------------------------

%NULL = 0
%LOCALE_SLONGDATE = &H20 ' long date format string
%LOCALE_STIMEFORMAT = &H1003 ' time format string
%LOCALE_USER_DEFAULT = &H0000
%DATE_SHORTDATE = &H1 ' use short date picture
%DATE_LONGDATE = &H2 ' use long date picture
%TIME_NOSECONDS = &H2& ' do not use seconds
%False = 0
%True = NOT(%False)


DIM TheName AS STRING
DIM TheEMail AS STRING
DIM TheComment AS STRING
DIM TheIP AS STRING
DIM TheDate AS STRING
DIM Keyword AS STRING
DIM Value AS STRING
DIM GBFile AS STRING
DIM TempFile AS STRING
DIM TempStr AS STRING
DIM TempLong AS LONG
DIM oFile AS LONG
DIM iFile AS LONG
DIM st AS LOCAL SYSTEMTIME
DIM d AS LOCAL ASCIIZ * 64
DIM t AS LOCAL ASCIIZ * 64
DIM s AS LOCAL ASCIIZ * 64

DIM i AS LONG
DIM p AS LONG
DIM j AS LONG

DIM Param(1) AS STRING


' change these as appropriate for your site(s)
' Also, look for the NAME command down below..

GBFile = "gbtext.dat"
TempFile = "gbtext.tmp"
ReturnURL = "/scripts/guestbk.exe"

' Read form contents
TempStr = ReadCGI

' Count and parse the parameters into an array
p = ParseParams(TempStr, Param()) ' p= number of parameters.
IF p < 1 THEN
STDOUT "Nothing to process!"
EXIT FUNCTION
END IF

' Get the values into our variables.

FOR i = 1 TO p
j = INSTR(Param(i), "=")
Keyword = UCASE$(LEFT$(Param(i), j-1))
Value = DecodeCGI(MID$(Param(i),j+1))
REPLACE "&" WITH "&" IN Value
REPLACE CHR$(34) WITH """ IN Value
REPLACE "<" WITH "<" IN Value
REPLACE ">" WITH ">" IN Value

IF KeyWord = "THENAME" THEN
TheName = Value
ELSEIF keyWord = "EMAIL" THEN
TheEMail = Value
ELSEIF keyword = "COMMENTS" THEN
TheComment = Value
ELSEIF KeyWord = "RETURNURL" THEN
ReturnURL = Value
END IF
NEXT i

' Now, add them to the top of the file.
' Yeah, it would be easier to put them at the bottom of the pile, but I
' want the newest ones to show up first.

i = 0
TempStr = ""
TempStr = DIR$(TempFile)
WHILE LEN(TempStr) > 0
INCR i
IF i > 10 THEN
STDOUT "Could not update guest book at this time"
EXIT FUNCTION
END IF
TempLong = TIMER + 1
WHILE TIMER < TempLong:
j = i
WEND
TempStr = DIR$(TempFile)
WEND

oFile = FREEFILE
OPEN TempFile FOR OUTPUT AS #oFile
WRITE #oFile, TheName, TheEMail, TheComment, Remote_Addr, DATE$

IF LEN(DIR$(GBFile)) > 0 THEN
iFile = FREEFILE
OPEN GBFile FOR INPUT AS #iFile

WHILE NOT EOF(iFile)
INPUT #iFile, TheName, TheEMail,TheCOmment, TheIP, TheDate
WRITE #oFile, TheName, TheEMail, TheComment, TheIP, TheDate
WEND
CLOSE #iFile
END IF
CLOSE #oFile

' OK, now kill the 'old' file and put the new one in it's place
KILL GBFile
NAME tempFile AS GBFile

' Write an ouput page for the user...
' Get date and time
GetLocalTime st
GetDateFormat %LOCALE_USER_DEFAULT, %DATE_LONGDATE, st, BYVAL %NULL, d, 64
GetDateFormat %LOCALE_USER_DEFAULT, %DATE_SHORTDATE, st, BYVAL %NULL, s, 64
GetTimeFormat %LOCALE_USER_DEFAULT, %TIME_NOSECONDS, st, BYVAL %NULL, t, 64

WriteCGI "<html>"
WriteCGI "<Head>"
WriteCGI "<TITLE>Thanks for signing the guest book</TITLE>
WriteCGI "<META HTTP-EQUIV=''Refresh'' CONTENT=''4; URL=" & ReturnURL & "''>"
WriteCGI "</HEAD>"
WriteCGI "<body TEXT=''#000000'' BGCOLOR=''#CCCCCC''>"
WriteCGI "<P><font face=''verdana,chicago,arial'' size=''4''>" & _
"GuestBook Entry received and processed " & t & ", " & d & " (" & s & ")</font></p>"
WriteCGI "<hr>"
WriteCGI "<P>You should be returned to the guest book in few seconds.</p>"
WriteCGI "<font face=''chicago,courier new,arial'' size=''2''>
WriteCGI "</BODY>"
WriteCGI "</HTML>"

END FUNCTION

------------------

IP: Logged

Bud Durland
Member
posted July 04, 2000 11:16 AM     Click Here to See the Profile for Bud Durland     Edit/Delete Message   Reply w/Quote
This is the code for GUESTBK.BAS :


$INCLUDE "PBCGI.INC"

FUNCTION PBMAIN

'+-------------------------------------------------------------------
' this part of the team reads the guestbook data file and
' delivers it as HTML data.
'
' Generally, this script is the entry point into the guest book
' with a like like so:
'
' /scripts/guestbk.exe
'
' It accepts 1 paramter (although you usually won't use it):
'
' NEXT=<some number>
'
' This is the first entry that will be displayed; This
' program displays %MaxEntries entrys at a time, and generates
' 'next' and 'previous' links on the page.
'+-------------------------------------------------------------------

DIM StartAt AS LONG
DIM Counter AS LONG
DIM GBFile AS STRING
DIM iFile AS LONG
DIM FlagEOF AS LONG
DIM FlagBOF AS LONG
DIM TheName AS STRING
DIM TheEMail AS STRING
DIM TheComment AS STRING
DIM TheIP AS STRING
DIM TheDate AS STRING
DIM SignPage AS STRING

DIM p AS LONG
DIM j AS LONG
DIM i AS LONG
DIM k AS LONG
DIM LastEntry AS LONG
DIM KeyWord AS STRING
DIM value AS STRING
DIM Scratch1 AS STRING
DIM Scratch2 AS STRING
DIM Scratch3 AS STRING
DIM Param(1) AS STRING

%MaxEntries = 10
%False = 0
%True = NOT(%False)

' Set some default values
StartAt = 1
FlagBOF = %False
FlagEOF = %False
GBFile = "gbtext.dat" ' this file stores the guest book entries
SignPage = "''/signbk.htm''" ' this is the name of the page where new
' guestbook entries can be made

' Get any secified options
Scratch1 = ReadCGI
p = ParseParams(Scratch1, Param()) ' p= number of parameters. (PBCGI.INC)
IF p > 0 THEN
j = INSTR(Param(1), "=")
Keyword = UCASE$(LEFT$(Param(1), j-1))
Value = DecodeCGI(MID$(Param(1),j+1))
IF Keyword = "NEXT" THEN
StartAt = VAL(Value)
END IF
END IF
'
' Write the HTML page header
'
WriteCGI "<html>"
WriteCGI "<head>"
WriteCGI "<meta http-equiv=''Content-Language'' content=''en-us''>"
WriteCGI "<meta NAME=''GENERATOR'' content=''PowerBasic Console Compiler''>"
WriteCGI "<title>Our Guest Book</title>"
WriteCGI "</head>"

WriteCGI "<body bgcolor=''#C0C0C0'' background=''/images/paperbackground.jpg''>
WriteCGI "<table border=''1'' width=''100%''>"
WriteCGI "<tr>"
WriteCGI " <td align=''center''>"
WriteCGI " <img border=''0'' src=''/images/handbook.gif''>"
WriteCGI " </td>"
WriteCGI " <td Align=''center''><font size=''+3''>Guest Book</font><br>"
WriteCGI " <font size=''+1''>Click <a href=" & SignPage & ">here</a> to sign our guest book</font><br><br>"
WriteCGI " <font face=''arial''>Entries are display in chronological order, with the newest ones first</font>"
WriteCGI" </td>"
WriteCGI "</tr>"
WriteCGI "</table>"
WriteCGI "<p> </p>"

'+---------------------------------------+
'| Now, read, format & display data |
'+---------------------------------------+
'
' is the file there?
IF LEN(DIR$(GBFile)) = 0 THEN
WriteCGI "<h3>The guest book is empty!<br>Be the first to sign!</h3>"
FlagBOF = %True
FlagEOF = %True
GOTO WrapUp ' Eek! a GOTO! but an appropriate one .
END IF

' Open the file, read to the first line to be displayed
IF StartAT = 1 THEN FlagBOF = %True
iFile = FREEFILE
OPEN GBFile FOR INPUT AS #iFile
i = StartAt
LastEntry = i - 1
FOR k = 1 TO i
INPUT #iFile, TheName, TheEMail, TheComment, TheIP, TheDate
NEXT k

Counter = 1
WHILE Counter <= %MaxEntries
Scratch1 = " "
Scratch2 = " "
INCR LastEntry
IF LEN(TheEMail) > 0 THEN
Scratch1 = " <a href=''mailto:" & TheEMail & "''>" & TheName & "</a> "
ELSE
Scratch1 = " " & TheName & " "
END IF
Scratch2 = "<p><h3>On " & TheDate & "," & Scratch1 & "wrote:</h2>"
WriteCGI Scratch2
WriteCGI "<blockquote>"
WriteCGI TheComment
WriteCGI "</blockquote>"
WriteCGI "<hr>"
IF EOF(iFile) THEN
FlagEOF = %True
EXIT LOOP
END IF
INPUT #iFile, TheName, TheEMail, TheComment, TheIP, TheDate
IF ERRCLEAR THEN
FlagEOF=%True
EXIT LOOP
END IF
INCR Counter
WEND

' All done, close the file
CLOSE #iFile

WrapUp:
'
' Figure out the required links and etc for the bottom
' of the page
'

i = (StartAt - %MaxEntries)
IF i < 1 THEN i = 1
j = StartAt + %MaxEntries
IF FlagBOF THEN
Scratch1 = "No Previous Entries"
ELSE
Scratch1 = "<a href=''/scripts/guestbk.exe?next=" & FORMAT$(i) & "''>"
Scratch1 = Scratch1 & "<<< Previous Entries</a>"
END IF

IF FlagEOF THEN
Scratch2 = "No More Entries"
ELSE
Scratch2 = "<a href=''/scripts/guestbk.exe?next=" & FORMAT$(j) & "''>"
Scratch2 = Scratch2 & "More Entries >>></a>"
END IF

IF LastEntry = 0 THEN
Scratch3 = " "
ELSE
Scratch3 = "<font=''arial''>You are viewing Guestbook Entries " & FORMAT$(StartAt)
Scratch3 = Scratch3 & " through " & FORMAT$(LastEntry) & "</font>"
END IF

WriteCGI "<p> </p>"
WriteCGI "<div align=''center''>"
WriteCGI "<table border=''0'' width=''70%''>
WriteCGI "<tr>"
WriteCGI " <td colspan=''3'' align=''center''>
WriteCGI " " & scratch3
WriteCGI " </td>"
WriteCGI "</tr>"
WriteCGI "<tr>"
WriteCGI " <td align=''left'' bgcolor=''yellow''><font face=''arial''>" & Scratch1 & "</font></td>"
WriteCGI " <td Align=''center'' bgcolor=''yellow''>"
WriteCGI " <font face=''arial''><a href=" & SignPage & ">Sign Our Guestbook</a></font>"
WriteCGI " </td>"
WriteCGI " <td align=''right'' bgcolor=''yellow''><font face=''arial''>" & Scratch2 & "</font></td>"
WriteCGI "</tr>"
WriteCGI "<tr>"
WriteCGI " <td colspan=''3'' align=''center''>"
WriteCGI " <font size=''+1''><a href=''http://www.cves.org''>Click here to return to our home page</a></font>"
WriteCGI " </td>"
WriteCGI "</tr>"
WriteCGI "</table></div>"
WriteCGI "</body></html>"

END FUNCTION


------------------

IP: Logged

All times are EasternTime (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | PowerBASIC BASIC Compilers

Copyright © 1999-2005 PowerBASIC, Inc. All Rights Reserved.


Ultimate Bulletin Board 5.45c