Jump to content
xisto Community
Sign in to follow this  
iGuest

Guestbook Script (Using Flat Files, no database)

Recommended Posts

Okay, I wrote a cool little Guestbook script a few weeks ago. It is very simple (one page of php) that uses flat files (text files). Using this script, you never need to use a database (you can if you want, but that's up to you). Anyways, here is the php code. Enjoy!

[br]<?php[br][/br]print "<form action='whateverthepageis.php' method='post'>";[/br]print "* Your name: " . "<input type='text' name='yourName'>";[br]print "Email: " . "<input type = 'text' name = 'email'><br>";[/br]print "Website URL: " . "<input type = 'text' name = 'webUrl'>";[br]print "Website Name: " . "<input type = 'text' name = 'webName'><br>"; [/br]print "* Comments: " . "<textarea name='comments'></textarea>";[br]print "<input type='submit' value='submit'>";[/br]print "</form>";[br][/br]print "<table>";[br]if(strlen($yourName) > 4 and strlen($comments) > 10)[/br]{[br]     $newEmail = "<a href='mailto:" . $email . "'>" . $email . "</a>";[/br]     $newWebUrl = "<a href='". $webUrl."'>". $webUrl . "</a>";[br][/br]     $fileID = fopen('post.txt', 'a');[br]     fputs($fileID, "Name: " . $yourName);[/br]     fputs($fileID, "Email: " . $newEmail);[br]     fputs($fileID, "Website Name: " . $webName);[/br]     fputs($fileID, "Website URL: " . $newWebUrl); [br]     fputs($fileID, "Comments: " . $comments); [/br]     fclose($fileID);[br][/br]     $fileID = fopen('post.txt', 'r');[br]     while(!feof($fileID))[/br]     {[br]           $gbookLine = fgets($fileID);[/br]           print "<tr><td>" . $gbookLine. "</td></tr>";[br]     }     [/br]     fclose($fileID);[br]}[/br]print "</table>";[br][/br]?>

By the way, if there is an error, I'll correct it.

Share this post


Link to post
Share on other sites

Here it is re-written for use with a MySQL database.

Please note that it is a simple, 'on-the-fly' script, so it mightn't be perfect or bugless.

I've also changed one or two things to suit the style in which I usually code. I hope you don't mind, Pandemonium.

<?php[br][/br]$sql_db = "guestbook";[br]$sql_user = "";[/br]$sql_pass = "";[br]// Make sure you set these variables. The rest can remain unedited,[/br]// assuming you don't want anything else changed.[br][/br]if(isset($_POST['yourName']) && isset($_POST['comments'])) {[br]	$name = $_POST['yourName'];[/br]	if(isset($_POST['email'])) { $email = $_POST['email']; }[br]	if(isset($_POST['webUrl'])) { $url = $_POST['webUrl']; }[/br]	if(isset($_POST['webName'])) { $website = $_POST['webName']; }[br]	$comments = $_POST['comments'];[/br]	[br]	mysql_connect("localhost", $sql_user, $sql_pass);[/br]	mysql_select_db($sql_db);[br]	mysql_query("INSERT INTO guestbook VALUES (0, '$name', '$email', '$url', '$website', '$comments')");[/br]	mysql_close();[br]}[/br]?>[br][/br]<p><form action="<?php echo("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']); ?>" method="post">[br]* Your name: <input type="text" name="yourName">[/br]Email: <input type = "text" name = "email"><br>[br]Website URL: <input type = "text" name = "webUrl">[/br]Website Name: <input type = "text" name = "webName"><br> [br]* Comments: <textarea name="comments"></textarea>[/br]<input type="submit" value="submit">[br]</form></p><p><hr></p><p>[br][/br]<?php[/br]mysql_connect("localhost", $sql_user, $sql_pass);[br]mysql_select_db($sql_db);[/br]$result = mysql_query("SELECT * FROM guestbook");[br]if($result) {[/br]	while($data = mysql_fetch_assoc($result)) {[br]  echo "<p><b>Name:</b> " . $data['name'] . "<br>\n";[/br]  if(isset($data['email'])) {[br]  	echo "<b>Email:</b> <a href=\"mailto:" . $data['email'] . "\">" . $data['email'] . "</a><br>\n";[/br]  }[br]  if(isset($data['url'])) {[/br]  	echo "<b>Website:</b> <a href=\"" . $data['url'] . "\" target=\"_BLANK\">";[br]  	if(isset($data['website'])) {[/br]    echo $data['website'];[br]  	} else {[/br]    echo $data['url'];[br]  	}[/br]  	echo "</a><br>\n";[br]  }[/br]  echo "<b>Comments:</b><br>\n" . $data['comments'];[br]  echo "</p>\n<hr>\n";[/br]	}[br]}[/br]mysql_close();[br][/br]echo "</p>";[br][/br]// Script originally created by Pandemonium for use with a flat-file database.[br]// Re-written for use with MySQL by Spectre.[/br]?>

Here is the table which I used as well (phpMyAdmin dump):

CREATE TABLE `guestbook` ([br]  `id` int(5) NOT NULL auto_increment,[/br]  `name` text NOT NULL,[br]  `email` varchar(30) NOT NULL default '',[/br]  `url` varchar(30) NOT NULL default '',[br]  `website` text NOT NULL,[/br]  `comments` text NOT NULL,[br]  PRIMARY KEY  (`id`)[/br]) TYPE=MyISAM AUTO_INCREMENT=4;

This query could be run directly from a PHP script if you want, as long as a connection to MySQL has been established and a database selected.

Keep up the good work, Pandemonium.

Share this post


Link to post
Share on other sites

Nice work there Spectre. And no, I don't mind if you changed some of it. :D But thanks for the compliment. I may post some more scripts sometime.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.