Jump to content

PHP Guestbook


stebbi

Recommended Posts

Hello

 

Need help with this please! I have the scrip for this guestbook just need some changes made.

 

1. Form and posts are on the same page want form to be on 1st page of guestbook and calls posts page on submit.

 

2. Want the newest posts on top of posts page.

 

3. Administrator Options is not working, (not so important)

 

Many thanks for your help with this.

 

 

Guestbook.php

<?php


/* Change this value regularly. */ 
$admin_password = "3007992569";

include "class_guestbook.php";

if(count($_GET) == 0){
/* This is where we display the guestbook, normally. No parameters have been specified*/
$guest = new guestbook;
$guest->set_file('guestbook_data.txt');
$guest->get_data();
$guest->display();	
}elseif(!empty($_GET['p'])){
/* There was a parameter set, figure out what it is and display the appropriate page. */
switch($_GET['p']){
	case 'post':
		/* Somebody is posting to the guestbook. Let's try to add the post */
		$guest = new guestbook;
		$guest->set_file('guestbook_data.txt');
		//$guest->import('data.html');
		//exit;
		$guest->get_data();
		//$guest->display(); 
		$data = $guest->show_data();
		$entry = new entry($data);
		$entry->new_entry();
		$entry->set_name(htmlentities($_POST['name']));
		$entry->set_location(htmlentities($_POST['location']));
		$entry->set_email(htmlentities($_POST['email']));
		$entry->set_homepage(htmlentities($_POST['homepage']));
		$entry->set_comments(nl2br(htmlentities($_POST['comments'])));
		$entry->set_date('', false);
		$entry->write_entry_data();
		$entry->write();
		$guest->set_data($entry->get_data());
		$guest->write();
		//echo $entry->show();
				header("Location: http://" . $_SERVER['HTTP_HOST']
                     . dirname($_SERVER['PHP_SELF'])
                     . "/guestbook.php");
            exit;
		break;
	case 'admin':
		session_start();
		/* Logging on as administrator, let's check the password. 
		 * This is hard coded into the program, so it's not going to be the
		 * most secure thing in the world. People WILL have problems getting
		 * in if they don't know it, though. */

		 if(!isset($_SESSION['pass']) && !isset($val)){
		 	/* Print the administrator login page */;	
		 	echo '<form action="./guestbook.php" method="GET">
						<input type="hidden" name="p" value="admin">
						<input type="hidden" name="val" value="1">
						Password: <input type="password" name="pass">
						<input type="submit" value="Administrator Options">
					</form>'; 
		 	exit;
		 }
		 if(@$_GET['pass'] == $admin_password){
		 		$_SESSION['pass'] = $admin_password;
		 }	
		 //if(!isset($_SESSION['pass'])){

		 //}
		 if($_SESSION['pass'] <> $admin_password){
		 	echo "ERROR. Access Denied. ";
		 	exit;
		 }else{
		 	/* Password was correct... Print the administrator options page. */
		 	$guest = new guestbook;
		 	$guest->set_file('guestbook_data.txt');
		 	$guest->get_data();
		 	$guest->display(true);
		 }
		 break;
	case 'modify':
		session_start();
		if($_SESSION['pass'] <> $admin_password){
			echo "ERROR. Access Denied.";
			exit;	
		}
		if(!isset($_GET['id']) || !isset($_GET['action'])){
			echo "ERROR. Not all information specified. Please go back and try again.";
			exit;	
		}
		switch($_GET['action']){
			case 'delete':
				$guest = new guestbook;
				$guest->set_file('guestbook_data.txt');
				$entry = new entry($guest->show_data());
				$entry->set_entry($_GET['id']);
				$entry->delete();
				$entry->write();
				$guest->set_data($entry->get_data());
				$guest->write();
				/* Now we'll forward back to the page we came from. */
				header("Location: http://" . $_SERVER['HTTP_HOST']
                     . dirname($_SERVER['PHP_SELF'])
                     . "/guestbook.php?p=admin");
				break;
			case 'change':
				/* We're here to change an entry... */
				$guest = new guestbook;
				$guest->set_file('guestbook_data.txt');
				//echo $guest->get_data();
				//exit;
				$entry = new entry($guest->show_data());
				$entry->set_entry($_GET['id']);
				$entry->set_name($_POST['name']);
				$entry->set_location($_POST['location']);
				$entry->set_email($_POST['email']);
				$entry->set_homepage($_POST['homepage']);
				$entry->set_comments($_POST['comments']);
				$entry->set_date($_POST['date']);
				$entry->write_entry_data();
				$entry->write();
				$guest->set_data($entry->get_data());
				$guest->write();	
				header("Location: http://" . $_SERVER['HTTP_HOST']
                     . dirname($_SERVER['PHP_SELF'])
                     . "/guestbook.php?p=admin");
				break;					
		}
}	
}
?>

 

class_guestbook.php

 

<?php


function file_put_contents($filename, $data){
/* For older PHP versions. Delete if using with a newer one. */
$handle = fopen($filename, "w");
$write = fwrite($handle, $data);
$close = fclose($handle);
}

class guestbook {

var $filename;
var $data;

function get_data(){
	$this->data = file_get_contents($this->filename);	
}

function show_data(){
	return $this->data;	
}

function set_data($data){
	$this->data = $data;
}

function write(){
	file_put_contents($this->filename, $this->data);	
}

function set_file($filename){
	if(file_exists($filename)){
		$this->filename = $filename;
	}else{
		file_put_contents($filename, "");
		$this->filename = $filename;
	}
	/* Place the data in the appropriate place */
	$this->get_data();
}

function get_filename(){
	return $this->filename;	
}

function entry_count(){
	return substr_count($this->data, '<entry_sep>');	
}

function display($admin=false){
	/* Goes through the entries, calling the display method on each. */
echo '	<HTML>
		<HEAD>
		<TITLE>Guestbook</TITLE>
		<STYLE TYPE="TEXT/CSS"><!--
		body,td{
			font-family:Comic Sans MS,Arial,Helvetica,Sans-serif;
		}
		--></STYLE>
		</HEAD>

		<BODY BGCOLOR="#FFFFCC" VLINK="#3366FF" LINK="#3366FF" TEXT="#000000">
		<DIV ALIGN="center"><FONT SIZE=6 COLOR=black><B>Guestbook</B></FONT></DIV>
		<HR>';
	$count = $this->entry_count();
	$entry = new entry($this->data);
	for($i=0;$i<$count;$i++){
		$entry->set_entry($i);
		echo $entry->display($admin);	
	}	
	if(!$admin){	
echo '<font size=4 color=black><b>Sign the Guestbook!</b></font>
		<p><form action="./guestbook.php?p=post" method="POST">
			<table border=0 cellpadding=2 cellspacing=0>
			<tr><td align=right><b>Name:</b></td><td><input maxlength="100" type="text" name="name" ></td></tr>
			<tr><td align=right><b>Email:</b></td><td><input maxlength="300"  type="text" name="email" va></td></tr>
			<tr><td align=right><b>HomePage:</b></td><td><input maxlength="300"  type="text" name="homepage" ></td></tr>
			<tr><td align=right><b>Where are<br>you from:</b></td><td valign=top><input  maxlength="100" type="text" name="location" ></td></tr>
			<tr><td valign=top align=right><b>Comments:</b></td><td width=500><textarea  maxlength="5000" rows=10 cols=40 name="comments"></textarea></td></tr>
			<tr><td></td><td><input type="submit" value="Sign the Guestbook!">		</td></tr>
			</table>
			</form>
		';

	echo '<p align="right"><a href="./guestbook.php?p=admin">Administrator Options</a></p>';
}
	echo '
		</body>
		</HTML>';
}

function import($datafile){
	 $data = file_get_contents($datafile);
	 $start = strpos($data, '<!-- Guestbook Insertion Point -->') + strlen('<!-- Guestbook Insertion Point -->');
	 $end = strpos($data, '<FORM');
	 $data = substr($data, $start, $end - $start);

	 /* Count the number of entries */
	 $count = substr_count($data, '<table border=0 cellpadding=2 cellspacing=0>');
	 $import_data = '';
	 for($i=0;$i<$count;$i++){
	 	$start = strpos($data, '<table border=0 cellpadding=2 cellspacing=0>') + strlen('<table border=0 cellpadding=2 cellspacing=0>');
	 	$end = strpos($data, '</table>');
	 	$data_entry = substr($data, $start, $end-$start);
	 	/* Get information specific to the entries */
	 			$sub_start = strpos($data_entry, '</b></td><td>') + strlen('</b></td><td>');
	 			$sub_end = strpos($data_entry, '</td></tr>');
	 		$name = substr($data_entry, $sub_start, $sub_end-$sub_start);
	 			$data_entry = substr($data_entry, $sub_end + strlen('</td></tr>'));
	 			$sub_start = strpos($data_entry, '</b></td><td>') + strlen('</b></td><td>');
	 			$sub_end = strpos($data_entry, '</a></td></tr>');
	 			$sub_start = strpos($data_entry, '>', $sub_start) + 1;
	 		$email = substr($data_entry, $sub_start, $sub_end-$sub_start);
	 			$data_entry = substr($data_entry, $sub_end + strlen('</td></tr>'));
	 			$sub_start = strpos($data_entry, '</b></td><td>') + strlen('</b></td><td>');
	 			$sub_end = strpos($data_entry, '</a></td></tr>');
	 			$sub_start = strpos($data_entry, '>', $sub_start) + 1;
	 		$url = substr($data_entry, $sub_start, $sub_end-$sub_start);
	 		if($url == "http://"){
	 			$url = '';
	 		}
	 			$data_entry = substr($data_entry, $sub_end + strlen('</td></tr>'));
	 			$sub_start = strpos($data_entry, '</b></td><td valign=top>') + strlen('</b></td><td valign=top>');
	 			$sub_end = strpos($data_entry, '</td></tr>');
	 		$location = substr($data_entry, $sub_start, $sub_end-$sub_start);
	 			$data_entry = substr($data_entry, $sub_end + strlen('</td></tr>'));
	 			$sub_start = strpos($data_entry, '</b></td><td width=500>') + strlen('</b></td><td width=500>');
	 			$sub_end = strpos($data_entry, '</td></tr>');
	 		$comments = substr($data_entry, $sub_start, $sub_end-$sub_start);
	 			$data_entry = substr($data_entry, $sub_end + strlen('</td></tr>'));
	 			$sub_start = strpos($data_entry, '<td><font size=2>') + strlen('<td><font size=2>');
	 			$sub_end = strpos($data_entry, '</font>');
	 		$date = substr($data_entry, $sub_start, $sub_end-$sub_start);
			$import_data = $import_data."<entry_sep>$name<sep>$email<sep>$url<sep>$location<sep>$comments<sep>$date</entry_sep>";
	 	$data = substr($data, $end + strlen('</table>'));	
	 }
	$exists = file_exists($this->filename);
			if($exists){
				$data = file_get_contents($this->filename);
				$data = $import_data.$data;
				$file_print = file_put_contents($this->filename, $data);
				if($file_print !== false){	
					echo "Written to: ".$this->filename;	
				}else{
					echo "Failed write. Make sure you have write access.";
				}
			}else{
				$file_print = file_put_contents($this->filename, $import_data);
				if($file_print !== false){
					echo "Written to: ".$this->filename;
				}else{
					echo "Failed write. Make sure you have write access.";
				}				
			}		 

}

}

class entry {
var $data;
var $entry_data;
var $entry;
var $date;
var $name;
var $location;
var $email;
var $homepage;
var $comments;

function entry($data){
	$this->data = $data;
}

function get_data(){
	return $this->data;	
}

function display($admin=false){
	/* This is where the formatting of the data begins. You should change this to 
	 * whatever you want... */
	 if(!$admin){
		 return '
			<table border=0 cellpadding=2 cellspacing=0>
			<tr><td align=right><b>Name:</b></td><td>'.$this->name.'</td></tr>
			<tr><td align=right><b>Email:</b></td><td><a href=mailto:"'.$this->email.'">'.$this->email.'</a></td></tr>
			<tr><td align=right><b>HomePage:</b></td><td><a href="'.$this->homepage.'">'.$this->homepage.'</td></tr>
			<tr><td align=right><b>Where are<br>you from:</b></td><td valign=top>'.$this->location.'</td></tr>
			<tr><td valign=top align=right><b>Comments:</b></td><td width=500>'.$this->comments.'</td></tr>
			<tr><td></td><td><font size=2>'.$this->date.'</font></td></tr>
			</table>
			<br>
			<hr size=2 width=80%>
			<br>
		';
	 }else{
	 	/* Administrator page... Print the guestbook entry with options */
		 return '<form action="./guestbook.php?p=modify&action=change&id='.$this->entry.'" method="POST">
			<a href="./guestbook.php?p=modify&action=delete&id='.$this->entry.'">Delete</a><br>
			<table border=0 cellpadding=2 cellspacing=0>
			<tr><td align=right><b>Name:</b></td><td><input maxlength="100" type="text" name="name" value="'.$this->name.'"></td></tr>
			<tr><td align=right><b>Email:</b></td><td><input maxlength="300"  type="text" name="email" value="'.$this->email.'"></td></tr>
			<tr><td align=right><b>HomePage:</b></td><td><input maxlength="300"  type="text" name="homepage" value="'.$this->homepage.'"></td></tr>
			<tr><td align=right><b>Where are<br>you from:</b></td><td valign=top><input  maxlength="100" type="text" name="location" value="'.$this->location.'"></td></tr>
			<tr><td valign=top align=right><b>Comments:</b></td><td width=500><textarea  maxlength="5000" rows=10 cols=40 name="comments">'.$this->comments.'</textarea></td></tr>
			<tr><td></td><td><font size=2><input type="text" name="date" value="'.$this->date.'"></font></td></tr>
			</table>
			<input type="submit" value="Change">
			</form>
			<br>
			<hr size=2 width=80%>
			<br>
		';		 		
	 }
}

function new_entry(){
	/* Count the number of entries */
	$count = substr_count($this->data, '<entry_sep>');
	$this->entry = $count;
	/* A new entry can now be written by setting appropriate fields. */
}

function set_entry($entry_id){
	/* This will look through the data, and set class variables for the specified entry. */
	$start = 0;
	for($i=0;$i<$entry_id + 1;$i++){
		$start = strpos($this->data, '<entry_sep>', $start) + strlen('<entry_sep>');
	}
	$end = strpos($this->data, '</entry_sep>', $start);
	$data = substr($this->data, $start, $end-$start);
	$this->entry_data = '<entry_sep>'.$data.'</entry_sep>';
	$data = explode('<sep>', $data);
	$this->entry = $entry_id;
	$this->name = $data[0];
	$this->email = $data[1];
	$this->homepage = $data[2];
	$this->location = $data[3];
	$this->comments = $data[4];
	$this->date = $data[5];
}

function delete(){
	$this->entry_data = '';
}	

function get_date(){
	return $this->date;
}

function set_date($date, $modify=true){
	if($modify){
		$this->date = $date;
	}else{
		$this->date = date("F j, Y, g:i a T");	
	}
}

function set_name($name){
	if(strlen($name) < 300){
		$this->name = $name;
	}else{
		echo "ERROR.";
		exit;
	}
}

function get_name(){
	return $this->name;
}

function set_location($location){
	if(strlen($location) < 150){
		$this->location = $location;
	}else{
		echo "ERROR.";
		exit;
	}
}

function get_location(){
	return $this->location;
}

function set_email($email){
	if(strlen($email) < 300){
		$this->email = $email;
	}else{
		echo "ERROR.";
		exit;
	}
}

function get_email(){
	return $this->email;
}

function set_homepage($homepage){
	if(strlen($homepage) < 300){
		$this->homepage = $homepage;
	}else{
		echo "ERROR.";
		exit;
	}
}

function get_homepage(){
	return $this->homepage;
}

function set_comments($comments){
	if(strlen($comments) < 5001){
		$this->comments = $comments;
	}else{
		echo "ERROR.";
		exit;
	}
}

function get_comments(){
	return $this->comments;
}

function write_entry_data(){
	$newarr = array($this->name, $this->email, $this->homepage, $this->location, $this->comments, $this->date);
	$this->entry_data = '<entry_sep>'.implode('<sep>', $newarr).'</entry_sep>';
}

function write(){
	/* Writes the changes made to this entry into the file. */
	$start = strpos($this->data, '<entry_sep>');
	for($i=0;$i<$this->entry;$i++){
		$start = strpos($this->data, '<entry_sep>', $start+4);
	}
	$end = strpos($this->data, '</entry_sep>', $start);
	/* Get the count of entries */
	$count = substr_count($this->data, '<entry_sep');
	if($this->entry == $count){
		$start = strlen($this->data);
		$end = strlen($this->data);	
	}
	/* We have to write the values between start and end... */
	$this->data = substr($this->data, 0, $start).$this->entry_data.substr($this->data, $end + strlen('</entry_sep>'));			
}

function show(){
	/* This will do the same as write, but will return the data, instead of writing it to the file. */	
	return $this->entry_data;
}

}

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.