Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 06-05-2011, 08:07 PM TASK 3: Guestbook
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Create a simple guestbook as a plugin for any website.


Required Features:

1. It can use MySQL, DBM or TXT (including delimited, csv, xml, etc) files for data storage.

2. It should include pagination.


Optional Features:

• Protect spam entries with captcha verification.

• Blacklist for email and/or IP addresses.

• Anything creative you can come up with that would enhance the guestbook application.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.

Last edited by mgraphic; 08-06-2011 at 08:57 PM..
mgraphic is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-04-2011, 05:16 PM Re: TASK 3: Guestbook
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Nice challenge, Keith!

I've come up with this:

(That's right, its one file )
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Text-Only Guestbook</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta name="author" content="Scott Kaye" />
<style type="text/css">body{font-family:Verdana;}input{min-height:18px;background:#fff;border-radius:10px;border:1px solid #000;color:#000;padding:5px;font-family:Verdana;box-shadow:inset 0 0 2px #555;font-size:80%;}[type="submit"]{float:right;}[type="submit"]:hover{background:#c9c9c9;}.pc{padding:5px 10px;background:#fef;border-bottom-left-radius:10px;border-bottom-right-radius:10px;}.ph{font-weight:700;background:#96A6FF;padding:5px 10px;border-top-left-radius:10px;border-top-right-radius:10px;}.pf{margin-bottom:10px;box-shadow:0px 3px 5px #888;border-radius:10px;}#con{color:#000;background:transparent;border:none;font-family:Verdana;font-size:80%;}#form{background:#fff;border-radius:10px;border:1px solid #000;padding:5px;box-shadow:inset 0 0 2px #555;display:table;}label{margin-right:10px;vertical-align:middle;font-weight:700;}</style>
</head>
<body>

<h1><a href="<?php echo basename(__FILE__);?>">Guestbook</a></h1>
<p>Post a visitor message!</p>
<form action="" method="post" id="form">
<textarea id="con" name="msg_content" rows="4" cols="80"></textarea><br />
<label>Your name:</label><input type="text" name="msg_author" size="55" />
<input type="submit" value="Submit" />
</form>
<hr />
<?php
//Settings//
$pag=3; //Pagination
$mf="togmsgs.txt"; //Message file
//End settings//
//Create the file, if it doesn't exist
if(!file_exists($mf)){$o=@fopen($mf,"a") or die("Folder not writable");fclose($o);}
//Get the file's contents as an array
$ms=file($mf);
//Check if the user is submitting a post or not
if(isset($_POST['msg_content'])){
	if($_POST['msg_content']!=null){
		$cf=array("badword","censorthis");
		$auth=$_POST['msg_author']!=null?$_POST['msg_author']:"Anonymous";
		$auth.=" on ".date("D jS M, Y, g:ia T",time());
		$auth=str_replace($cf,"***",str_replace("|","-",str_replace("\r\n","<br />",$auth)));
		$cont=str_replace($cf,"***",str_replace("|","-",str_replace("\r\n","<br />",$_POST['msg_content'])));
		$prev=file_get_contents($mf);
		$f=fopen($mf,"w");
		fwrite($f,$auth."|".$cont."\n".$prev);
		fclose($f);
		header("Location:".$_SERVER['HTTP_REFERER']);
	}
	else {echo "You didn't write anything!";}
}
//Set some variables first
if($_GET['p']<=0&&isset($_GET['p'])){header("Location:?p=1");}
$cp=isset($_GET['p'])?$_GET['p']:1;
//Loop through it and display each message.
for($i=$cp-1;$i<=($cp+$pag)-2;$i++){
	list($a,$c)=explode("|",$ms[$i]);
	if($a!=null&&$c!=null){echo "<div class=\"pf\"><div class=\"ph\">$a</div><div class=\"pc\">$c</div></div>";}
	$n=$i+2;
}
//Pagination
if($cp>1){$sp=1;}if($cp<count($ms)){$sn=1;}
if($sp==1){echo "<a href=\"?p=".($cp-$pag)."\">Previous Page</a>";}
if($sn==1){if($sp==1){echo "&nbsp;|&nbsp";}echo "<a href=\"?p=$n\">Next Page</a>";}
?>

</body>
</html>
It runs on a flat file, and filters out your choice of bad words. It has simple pagination, too.

Here's the uncommented version:

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>Text-Only Guestbook</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta name="author" content="Scott Kaye" />
<style type="text/css">body{font-family:Verdana;}input{min-height:18px;background:#fff;border-radius:10px;border:1px solid #000;color:#000;padding:5px;font-family:Verdana;box-shadow:inset 0 0 2px #555;font-size:80%;}[type="submit"]{float:right;}[type="submit"]:hover{background:#c9c9c9;}.pc{padding:5px 10px;background:#fef;border-bottom-left-radius:10px;border-bottom-right-radius:10px;}.ph{font-weight:700;background:#96A6FF;padding:5px 10px;border-top-left-radius:10px;border-top-right-radius:10px;}.pf{margin-bottom:10px;box-shadow:0px 3px 5px #888;border-radius:10px;}#con{color:#000;background:transparent;border:none;font-family:Verdana;font-size:80%;}#form{background:#fff;border-radius:10px;border:1px solid #000;padding:5px;box-shadow:inset 0 0 2px #555;display:table;}label{margin-right:10px;vertical-align:middle;font-weight:700;}</style>
</head><body><h1><a href="<?php echo basename(__FILE__);?>">Guestbook</a></h1>
<p>Post a visitor message!</p>
<form action="" method="post" id="form">
<textarea id="con" name="msg_content" rows="4" cols="80"></textarea><br />
<label>Your name:</label><input type="text" name="msg_author" size="55" />
<input type="submit" value="Submit" />
</form>
<hr />
<?php
$pag=3;
$mf="togmsgs.txt";
if(!file_exists($mf)){$o=@fopen($mf,"a") or die("Folder not writable");fclose($o);}
$ms=file($mf);
if(isset($_POST['msg_content'])){
	if($_POST['msg_content']!=null){
		$cf=array("badword","censorthis");
		$auth=$_POST['msg_author']!=null?$_POST['msg_author']:"Anonymous";
		$auth.=" on ".date("D jS M, Y, g:ia T",time());
		$auth=str_replace($cf,"***",str_replace("|","-",str_replace("\r\n","<br />",$auth)));
		$cont=str_replace($cf,"***",str_replace("|","-",str_replace("\r\n","<br />",$_POST['msg_content'])));
		$prev=file_get_contents($mf);
		$f=fopen($mf,"w");
		fwrite($f,$auth."|".$cont."\n".$prev);
		fclose($f);
		header("Location:".$_SERVER['HTTP_REFERER']);
	}
	else {echo "You didn't write anything!";}
}
if($_GET['p']<=0&&isset($_GET['p'])){header("Location:?p=1");}
$cp=isset($_GET['p'])?$_GET['p']:1;
for($i=$cp-1;$i<=($cp+$pag)-2;$i++){
	list($a,$c)=explode("|",$ms[$i]);
	if($a!=null&&$c!=null){echo "<div class=\"pf\"><div class=\"ph\">$a</div><div class=\"pc\">$c</div></div>";}
	$n=$i+2;
}
if($cp>1){$sp=1;}if($cp<count($ms)){$sn=1;}
if($sp==1){echo "<a href=\"?p=".($cp-$pag)."\">Previous Page</a>";}
if($sn==1){if($sp==1){echo "&nbsp;|&nbsp";}echo "<a href=\"?p=$n\">Next Page</a>";}?></body></html>
Hope it's useful
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is online now
Reply With Quote
View Public Profile
 
Old 08-04-2011, 11:04 PM Re: TASK 3: Guestbook
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
You are not allowed to set headers after output from line 41
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 08-04-2011, 11:10 PM Re: TASK 3: Guestbook
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Wow, nice call Keith! I forgot about that.

That's weird though. Normally PHP will get mad at you for doing that, and exit() out with a fatal error (headers already sent). I wonder why it didn't tell me, and why I didn't see that.

Thanks for pointing that out!
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is online now
Reply With Quote
View Public Profile
 
Reply     « Reply to TASK 3: Guestbook
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.73056 seconds with 12 queries