Jump to content

[SOLVED] Max's Guestbook


tronicsmasta

Recommended Posts

Hey guys,

 

I usually don't request help with php unless I am scratching my head like tonight lol.

 

I am using a copy of Max's Guestbook... All worked ok when the files were all in the same directory as the .html file that called the scripts. I have attempted, yes keyword attempted, to modify the script to use a different directory on the server... no luck...

 

Directory Structure I am attempting to use:

/channel.html

/video/1_channel/

/video/1_channel/comments.class.php

/video/1_channel/comments/  (chmodded to 777)

/video/1_channel/comments/05032008195332_IP(hidden).txt  (example comment in the directory)

 

When channel.html, comments.class.php, and comments/ where in the same directory no bubbles no troubles...

When it follows the directory structure above... no go...

 

I tried assuming that the script would call for folders/files in its directory because the script runs under its own dir...

eg. (comments.class.php would call files under the comments/ folder when referenced to its own directory...)

 

However this is not the case... I tried changing the commentsDir to /video/1_channel/comments and still no go... idk why?

 

Anyhow, if you experts can assist me that would be great. My code is below.

 

Embeded code into channel.html:

<?php 
   require_once("video/1_channel/comments.class.php"); 
   $myComments = new maxComments(); 
   $myComments->processComments();         
?>

 

comments.class.php code:

<?php
class maxComments{
   var $commentsDir = 'comments';
   var $dateFormat = 'm.d.Y g:i A';
   var $itemsPerPage = 5;
   var $commentsList;
   
function processComments(){
   if (isset($_POST['submit'])) {
      $this->insertComments();
   }
   $page = isset($_GET['page']) ? $_GET['page'] : 1;
   
   $this->displayComments($page);
}
   
function getCommentsList(){

   $this->commentsList = array();
   
// Open the actual directory
if ($handle = @opendir($this->commentsDir)) {
	// Read all file from the actual directory
	while ($file = readdir($handle))  {
	    if (!is_dir($file)) {
	       $this->commentsList[] = $file;
      	}
	}
}	

rsort($this->commentsList);

return $this->commentsList;
}   

function displayComments($page=1){
      $list = $this->getCommentsList();
      //echo "<center><a href='add.php'>Leave a comment</a></center>";
      echo "<div class='comments'>";
      
      //Get start point and end point
      $startItem = ($page-1)*$this->itemsPerPage;
      if (($startItem + $this->itemsPerPage) > sizeof($list)) $endItem = sizeof($list);
      else $endItem = $startItem + $this->itemsPerPage; 
      
      for ($i=$startItem;$i<$endItem;$i++){
         //foreach ($list as $value) {
         $value = $list[$i];
      	$data = file($this->commentsDir.DIRECTORY_SEPARATOR.$value);
      	$name  = trim($data[0]);
      	$email = trim($data[1]);
         $submitDate = trim($data[2]);	
         unset ($data['0']);
         unset ($data['1']);
         unset ($data['2']);
      	
         $content = "";
         foreach ($data as $value) {
    	       $content .= $value;
         }
      	
      	echo "<ul><li><h3>$submitDate  by  <span id=\"name\">$name</span></h3>";
      	echo "    ".nl2br(htmlspecialchars($content))."<br/></li></ul>";
      }
      echo "</div>";
      if (sizeof($list) == 0){
         echo "<div id=\"comments\"><ul><li><h3>No comments at the moment!</h3></li></ul></div>";
      }
      // Create pagination
      if (sizeof($list) > $this->itemsPerPage){
         echo "<div id=\"commentNav\">";
         if ($startItem == 0) {
            if ($endItem < sizeof($list)){
               echo "<div id=\"nright\"><a href=\"".$_SERVER['PHP_SELF']."?page=".($page+1)."\" >Next »</a></div>";
            } else {
               // Nothing to display
            }
         } else {
            if ($endItem < sizeof($list)){
               echo "<div id=\"nleft\"><a href=\"".$_SERVER['PHP_SELF']."?page=".($page-1)."\" >« Prev</a></div>";
               echo "<div id=\"nright\"><a href=\"".$_SERVER['PHP_SELF']."?page=".($page+1)."\" >Next »</a></div>";
            } else {
               echo "<div id=\"nleft\"><a href=\"".$_SERVER['PHP_SELF']."?page=".($page-1)."\" >« Prev</a></div>";
            }
         }
         
         echo "<br/></div><br/>";
      }
      echo "<hr />";
      $this->displayAddForm();
}

function displayAddForm(){
?> <div id="form"> 
  <form class="iform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Name:<br/>
    <input type="text" name="name" size="30"/><br/><br/>
    Comment:<br/>
    <textarea name="comments" rows="7" cols="49"></textarea><br/>
<input type="submit" name="submit" value="Leave Comment" />
  </form> 
   </div>
<?php   
}

function insertComments(){
   $name   = isset($_POST['name']) ? $_POST['name'] : 'Anonymous';
   $email  = isset($_POST['email']) ? $_POST['email'] : '';
   $ip     = $_SERVER['REMOTE_ADDR']; 
   $submitDate  = date($this->dateFormat);
   $content = isset($_POST['comments']) ? $_POST['comments'] : '';
   
   if (trim($name) == '') $name = 'Anonymous';
   if (strlen($content)<5) {
      exit();
   }
   
   $filename = date('mdYHis')."_$ip";
   if (!file_exists($this->commentsDir)){
      mkdir($this->commentsDir);
   }
   $f = fopen($this->commentsDir.DIRECTORY_SEPARATOR.$filename.".txt","w+");         
   fwrite($f,$name."\n");
   fwrite($f,$email."\n");
   fwrite($f,$submitDate."\n");
   fwrite($f,$content."\n");
   fclose($f);
   
}
}
?>

 

Even when i put a manual comment in the comments directory, it says no comments... when i add a comment the script fails and stops the rest of the html from processing...

 

any ideas?

 

Thank you!!!

 

Quinton

Link to comment
Share on other sites

Look in the comments class file. Change  (it's near the first line):

   var $commentsDir = 'comments';

To:

   $commentsDir = 'video/1_channel/comments';

You don't need the var declaration.

This will work if it's going to be static.  Otherwise, you'll need to do some changing to the class.  But if comments are going to ONLY be in that directory, that should work. =)

 

Link to comment
Share on other sites

i am new to classes and am not sure about some of the code.

I figured out I was tired and uploading the wrong copy of the file... so now i read the comments in the comments folder, but when I click add comment to submit my comment... the php script errors and i get nothing from that part of the page down...

 

I tried this

class maxComments{
   var $commentsDir = 'video/1_channel/comments';
   var $dateFormat = 'm.d.Y g:i A';
   var $itemsPerPage = 5;
   var $commentsList;

 

and also tried

 

$commentsDir = 'video/1_channel/comments';
class maxComments{
   var $dateFormat = 'm.d.Y g:i A';
   var $itemsPerPage = 5;
   var $commentsList;

 

and no go...

Link to comment
Share on other sites

Change:

function insertComments(){
   $name   = isset($_POST['name']) ? $_POST['name'] : 'Anonymous';
   $email  = isset($_POST['email']) ? $_POST['email'] : '';
   $ip     = $_SERVER['REMOTE_ADDR']; 
   $submitDate  = date($this->dateFormat);
   $content = isset($_POST['comments']) ? $_POST['comments'] : '';
   
   if (trim($name) == '') $name = 'Anonymous';
   if (strlen($content)<5) {
      exit();
   }
   
   $filename = date('mdYHis')."_$ip";
   if (!file_exists($this->commentsDir)){
      mkdir($this->commentsDir);
   }
   $f = fopen($this->commentsDir.DIRECTORY_SEPARATOR.$filename.".txt","w+");         
   fwrite($f,$name."\n");
   fwrite($f,$email."\n");
   fwrite($f,$submitDate."\n");
   fwrite($f,$content."\n");
   fclose($f);
   
}

To:

function insertComments(){
   $name   = isset($_POST['name']) ? $_POST['name'] : 'Anonymous';
   $email  = isset($_POST['email']) ? $_POST['email'] : '';
   $ip     = $_SERVER['REMOTE_ADDR']; 
   $submitDate  = date($this->dateFormat);
   $content = isset($_POST['comments']) ? $_POST['comments'] : '';
   
   if (trim($name) == '') $name = 'Anonymous';
   if (strlen($content)<5) {
      exit();
   }
   
   $filename = date('mdYHis')."_$ip";
   if (!file_exists($this->commentsDir)){
      mkdir($this->commentsDir);
   }
   $f = fopen($this->commentsDir.DIRECTORY_SEPARATOR.$filename.".txt","w+");         
   fwrite($f,$name."\n");
   fwrite($f,$email."\n");
   fwrite($f,$submitDate."\n");
   fwrite($f,$content."\n");
   fclose($f);
   echo "Comment added successfully";  
}

 

Now, if it succeeds, it'll show something.  And you can actually VIEW the comments now?

Link to comment
Share on other sites

the comments do show... look at the link i pm'ed you... but, when I submit the form below, i get nothing at all

 

when it works, you click submit and after the refresh, the comment shows right away. basically it stops processing now...

 

eg from view source of the page...

<!-- comments -->

<div id="comments">

<h2>Comments</h2>

the PHP output is usually here!

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.