| px | top | add code | search | signup | login | help |
<?php
/*
*************************************************
* Common Information about the guestbook module *
*************************************************
Copyright information:
----------------------
This module was coded by nevarsa.com (info@nevarsa.com) and can be used for free.
Please understand, that we will not answer any questions concerning this module
If you add additional cool features or fix a bug, please email the modified
script to info@nevarsa.com
Feel free to modify and use this module according to your requirements.
Install information:
--------------------
This module needs a mySQL database for storing the entries.
Use the following SQL query to create the correct table within your database.
CREATE TABLE guestbook (
id int(11) NOT NULL auto_increment,
posted date DEFAULT \'0000-00-00\' NOT NULL,
name varchar(50) NOT NULL,
email varchar(40),
message text,
KEY posted (posted),
KEY id (id)
);
It is recommended to store the login data for the mySQL database in a separate include file.
Use this code for your \"settings.inc\"
<?
// *********************
// * Database Settings *
// *********************
$dbserver = \"localhost\"; // Servername
$dbuser = \"user\"; // Username
$dbpass = \"password\"; // Password
$db = \"database\"; // Database
?>
For additional installation help refer to the PHP and mySQL manuals at www.php.net and www.mysql.com
*/
// ********************
// * Show the entries *
// ********************
class guestbook {
function show_entries() {
include "settings.inc\"; // Open DB
$dblink = mysql_pconnect($dbserver,$dbuser,$dbpass);
mysql_select_db($db,$dblink);
$result = mysql_query(\"SELECT * FROM guestbook ORDER BY id desc\");
$amount = mysql_numrows($result);
if ($amount == 0) { // No entries found: show error message
?>
<p><font size=2><b>Error: No entries were found.</b></font></p>
<?
} else { // Entries found: show them
?>
<p><font size=2><b><? echo $amount; ?> Entries found:<br> </b></font></p>
<table border=0 cellpadding=0 cellspacing=0 width=450>
<?
while ($entry = mysql_fetch_object($result)) {
$date = explode(\"-\",$entry->posted); // Get date of entry
$year = $date[0];
$month = $date[1];
$day = $date[2];
$date = $day.\".\".$month.\".\".$year;
?>
<tr>
<td width=50><font size=2><b><? echo $date; ?></b></font></td>
<td width=270><font size=2><b><a href=mailto:<? echo $entry->email; ?>><? echo $entry->name; ?></a></b></font></td>
<td width=130 align=right><font size=2><b><a href=#form>Create new entry...</a></b></font></td>
</tr>
<tr>
<td colspan=3><hr color=#EEEEEE size=1 noshade></font></td>
</tr>
<tr>
<td colspan=3><font size=2><? echo $entry->message; ?></font></td>
</tr>
<tr>
<td colspan=3><hr color=#EEEEEE size=1 noshade></font></td>
</tr>
<?
}
?>
</table>
<?
}
mysql_close($dblink); // Close DB
}
// *******************
// * Check the entry *
// * return error *
// *******************
function check_entries ($name, $email, $message) {
if ($name == \"\") {
$this->error[check] = \"true\";
$this->error[name] = \"true\";
}
if ($email == \"\") {
$this->error[check] = \"true\";
$this->error[email] = \"true\";
} else if (!eregi(\"^[_\\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\\.)+[a-z]{2,3}$\",$email)) {
$this->error[check] = \"true\";
$this->error[email] = \"true\";
}
if ($message == \"\") {
$this->error[check] = \"true\";
$this->error[message] = \"true\";
}
return $this->error;
}
// *******************
// * Store the entry *
// *******************
function store_entries($name, $email, $message) {
$date = getdate();
$year = $date[year];
$month = $date[mon];
$day = $date[mday];
$posted = $year.\"-\".$month.\"-\".$day;
include \"settings.inc\"; // Open DB
$dblink = mysql_pconnect($dbserver,$dbuser,$dbpass);
mysql_select_db($db,$dblink);
$result = mysql_query(\"INSERT INTO guestbook (name, email, message, posted) VALUES (\'$name\', \'$email\', \'$message\', \'$posted\')\");
mysql_close($dblink); // Close DB
}
// *****************
// * Show the form *
// *****************
function show_form($error, $name, $email, $message) {
?>
<form method=post action=guestbook.php>
<input type=hidden name=filled value=true>
<table border=0 cellpadding=0 cellspacing=0 width=450>
<tr>
<td colspan=2><font size=2><b><a name=form>Create new entry:</a></b></font></td>
</tr>
<tr>
<td colspan=2><hr color=#EEEEEE size=1 noshade></font></td>
</tr>
<tr>
<td><font size=2<? if ($error[name] == \"true\") echo \" color=#A12B2B\"; ?>><b>Name:</b></font></td>
<td><input type=text name=name value=\"<? echo $name; ?>\" size=40></td>
</tr>
<tr>
<td><font size=2<? if ($error[email] == \"true\") echo \" color=#A12B2B\"; ?>><b>E-Mail:</b></font></td>
<td><input type=text name=email value=\"<? echo $email; ?>\" size=40></td>
</tr>
<tr>
<td valign=top><font size=2<? if ($error[message] == \"true\") echo \" color=#A12B2B\"; ?>><b>Message:</b></font></td>
<td><textarea rows=6 name=message cols=30><? echo $message; ?></textarea></td>
</tr>
<tr>
<td colspan=2><hr color=#EEEEEE size=1 noshade></font></td>
</tr>
</table>
<input type=submit value=send>
</form>
<?
}
}
// *******************
// * function main() *
// *******************
if ($filled != \"true\") { // Form was not yet filled: show entries, show form
$guestbook = new guestbook;
$guestbook->show_entries();
$guestbook->show_form(\"\", \"\", \"\", \"\");
} else { // Form was filled: check entry
$guestbook = new guestbook;
$guestbook->check_entries($name, $email, $message);
if ($guestbook->error[check] == \"true\") { // Entry is incomplete: show error message
?>
<p><font size=2><b>An error occured while posting your entry. Please check the marked fields.<br> </b></font>
<?
$guestbook->show_form($guestbook->error, $name, $email, $message);
} else { // Entry is complete: store in DB, show entries
$guestbook->store_entries($name, $email, $message);
?>
<p><font size=2><b>Your entry was posted successfully.<br> </b></font>
<?
$guestbook->show_entries();
$guestbook->show_form(\"\", \"\", \"\", \"\");
}
}
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.