We help IT Professionals succeed at work.

GuestBook

assaultkitty
assaultkitty asked
on
I am having problems with the Show Guest Book.  I have a problem with the fclose and fopen.  Can you help me with this problem?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Show Guest Book</title>
<meta http-equiv="content-type"
      content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
$Book=fopen("guestbook.txt", "ab");
if (is_readable($Book))
{
    $GuestBook=readfile("guestbook.txt");
    echo "<pre>$GuestBook</pre>";
}
else
    echo "<p>The $Book file cannot be read.</p>";

fclose($Book);
?>
<hr /><p>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sign Guest Book</title>
<meta http-equiv="content-type"
      content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
if (empty($_POST['first_name']) || empty($_POST
['last_name']))
      echo "<p>You must enter your first and last
      name.  Click your browser's Back button to
      return to the Guest Book.</p>\n";
else
{
      $FirstName = addslashes($_POST['first_name']);
      $LastName = addslashes($_POST['last_name']);
      $GuestName = fopen("guestbook.txt", "ab");
      if (is_writeable("guestbook.txt"))
      {
            if (fwrite($GuestBook, $LastName . ", " .
                   $FirstName . "\n"))
            echo "<p>Thank you for signing our
                  guest book!</p>\n";
            else
                  echo "<p>Cannot add your name to the
                        guest book.</p>\n";
      }
      else
            echo "<p>Cannot write to the file.</p>\n";
      fclose($GuestBook);
}
?>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Guest Book</title>
<meta http-equiv="content-type"
      content="text/html; charset=iso-8859-1" />
</head>
<body>
<h2>Enter your name to sign our guest book</h2>
<form method= "POST action="SignGuestBook.php">
<p>First Name<input type="text" name="first_name"
/></p>
<p>Last Name<input type="text" name="last_name"
/></p>
<p><input type="submit" value="Submit" /></p>
</form>
<p><a href ="ShowGuestBook.php">Show Guest Book
</a></p>
</body>
</html>
Comment
Watch Question

Commented:
Try this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Show Guest Book</title>
<meta http-equiv="content-type"
      content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
$Book="guestbook.txt";
if (is_readable($Book)) 
{
    $GuestBook=readfile("guestbook.txt");
    echo "<pre>$GuestBook</pre>";
}
else
    echo "<p>The $Book file cannot be read.</p>";
?>
<hr /><p>
</body>
</html>

Open in new window

Author

Commented:
It is working now.  But, I am unable to read the guestbook.txt file.  What do I need to do get this part to work?

Commented:
according to your work, which part?

Author

Commented:
First, I created the guestbook.txt.  When I input my name into the GuestBook.html, I do not get a message saying thank you for signing the Guest book or cannot add your name to the guest book.  When i click show guest book, I this message The guestbook.txt file cannot be read.  I made sure that the permission for the guestbook.txt is full control.  Can you help me?  

Commented:
Ok, in the file SignGuestBook.php should be something like this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sign Guest Book</title>
<meta http-equiv="content-type"
      content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
if (empty($_POST['first_name']) || empty($_POST
['last_name']))
      echo "<p>You must enter your first and last
      name.  Click your browser's Back button to
      return to the Guest Book.</p>\n";
else
{
      $FirstName = htmlspecialchars($_POST['first_name']); // I changed addslashes to htmlspecialchars since you are not add them into database but a text file and read it straightforward
      $LastName = htmlspecialchars($_POST['last_name']); // I changed here too
      if (is_writeable("guestbook.txt"))
      {
            /* I moved fopen and fclose into this block, since you should check if it is writeable or not first. */
            $GuestName = fopen("guestbook.txt", "ab");
            if (fwrite($GuestBook, $LastName . ", " .
                   $FirstName . "\n"))
            echo "<p>Thank you for signing our 
                  guest book!</p>\n";
            else
                  echo "<p>Cannot add your name to the 
                        guest book.</p>\n";
            fclose($GuestBook);
      }
      else
            echo "<p>Cannot write to the file.</p>\n";
}
?>
</body>
</html>

Open in new window

Commented:
One more thing is making sure that guestbook.txt is writeable or you will not be able to overwrite it.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Good news!  The PHP web site has online documentation for all the native functions, so you will never again make a newbie mistake like misapplication of a function.  You can just look it up  on PHP.net and see how it is supposed to be used.  Note that fopen() returns a file pointer resource on success, or FALSE on error.
http://php.net/manual/en/function.fopen.php

However is_readable() expects a file name.
http://php.net/manual/en/function.is-readable.php

You will want to look this one up, too.
http://php.net/manual/en/function.clearstatcache.php

Suggest you set error_reporting(E_ALL) at the top of your PHP scripts.  At this point in the application lifecycle you want to see all the notices you can get.

HTH, ~Ray

Author

Commented:
Well I am not getting the the file is not readable anymore.  The only output I am getting is an o.  I checked to make sure my file was readable, writeable and executable.  I have full control on the txt file.  Can you help me? Nothing is working right now.

Author

Commented:
I read the book and this is the code for that I should use.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Show Guest Book</title>
<meta http-equiv="content-type"
      content="text/html; charset=iso-8859-1" />
</head>
<body>
      <pre>
            <?php readfile("guestbook.txt"); ?>
      </pre>
<hr /><p>
</body>
</html>

This is the code I should use.  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Guest Book</title>
<meta http-equiv="content-type"
      content="text/html; charset=iso-8859-1" />
</head>
<body>
<h2>Enter your name to sign our guest book</h2>
<form method= "POST action="SignGuestBook.php">
<p>First Name<input type="text" name="first_name"
/></p>
<p>Last Name<input type="text" name="last_name"
/></p>
<p><input type="submit" value="Submit" /></p>
</form>
<p><a href ="ShowGuestBook.php">Show Guest Book
</a></p>
</body>
</html>

This is the code I should use according to the book.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sign Guest Book</title>
<meta http-equiv="content-type"
      content="text/html; charset=iso-8859-1" />
</head>
<body>

<?php

if (empty($_GET['first_name']) || empty($_GET['last_name']))
echo "<p>You must enter your first and last name!
Click your browser's Back button to return to
the Guestbook.</p>";
else {
$FirstName = addslashes($_GET['first_name']);
$LastName = addslashes($_GET['last_name']);
$GuestBook = fopen("guestbook.txt", "a");
if (is_writable("guestbook.txt")) {
if (fwrite($GuestBook, $LastName . ", "
. $FirstName . "\n"))
echo "<p>Thank you for signing our guest book!</p>";
else
echo "<p>Cannot add your name to the guest book.</p>";
}

else
echo "<p>Cannot write to the file.</p>";
fclose($GuestBook);
}
?>
</body>
</html>

I am still having problems.  I have made siure the permissions are correct for the server.
Most Valuable Expert 2011
Top Expert 2016
Commented:
Here are two scripts that make sense to me.  You can find your way to them on my server here.
http://www.laprbass.com/RAY_temp_assaultkitty_display.php 
<?php // RAY_temp_assaultkitty_display.php
error_reporting(E_ALL);

// SET THE NAME OF THE GUESTBOOK
$book = 'RAY_temp_assaultkitty.txt';

// SET OUR VARIABLES
$htm_top = <<<HTM_TOP
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />
<title>AssaultKitty Display Guest Book</title>
</head>
<body>
<pre>
HTM_TOP;

$htm_end = <<<HTM_END
</pre>
<hr />
<p><a href="RAY_temp_assaultkitty_update.php">Sign Guest Book</a></p>
</body>
</html>
HTM_END;

// CREATE THE OUTPUT
ob_start();
echo $htm_top;

// GET THE GUESTBOOK
readfile($book);

// WRAP UP THE WEB PAGE
echo $htm_end;

Open in new window

<?php // RAY_temp_assaultkitty_update.php
error_reporting(E_ALL);

// SET THE NAME OF THE GUESTBOOK
$book = 'RAY_temp_assaultkitty.txt';

// SET OUR OUTPUT STRING VARIABLES
$htm_top = <<<HTM_TOP
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />
<title>AssaultKitty Update Guest Book</title>
</head>
<body>
HTM_TOP;

$htm_end = <<<HTM_END
<hr />
<p><a href="RAY_temp_assaultkitty_display.php">Show Guest Book</a></p>
</body>
</html>
HTM_END;

$htm_mid = NULL;


// IF THERE IS MISSING INPUT
if (empty($_POST['first_name']) || empty($_POST['last_name']))
{
    $htm_mid .= "<p>Please enter your first and last name in this form</p>";
    $htm_mid .= <<<HTM_MID
<form method= "post">
<p>First Name <input name="first_name" /></p>
<p>Last Name  <input name="last_name"  /></p>
<p><input type="submit" value="Submit" /></p>
</form>
HTM_MID;

}

// ACTION SCRIPT: PROCESS THE INPUT
else
{
    // CREATE THE GUESTBOOK DATA STRING
    $str = htmlentities($_POST['first_name']) . ', ' . htmlentities($_POST['last_name']) . PHP_EOL;

    // OPEN AND WRITE THE GUESTBOOK
    $gbp = fopen($book, "a");
    if (!$gbp)               die("FATAL ERROR ON FOPEN  $book FOR 'a' ");
    if (!fwrite($gbp, $str)) die("FATAL ERROR ON FWRITE $book FOR 'a' ");

    $htm_mid .= "<p>Thank you for signing our guest book!</p>";
    fclose($gbp);
}



// CREATE THE OUTPUT
ob_start();
echo $htm_top;
echo $htm_mid;
echo $htm_end;

Open in new window

Author

Commented:
Ray, this is wonderful.  Can you explain a little more what you actually did to this program?  I am a beginner and really would like to learn what is happening here?

Author

Commented:
Also, can you direct me on how to use my programs to accomplish this same program?
Most Valuable Expert 2011
Top Expert 2016

Commented:
Please feel free to copy the scripts here, install them on your own server and tinker with them to see how they behave.  

A good learning resource (or two) here...  Buy the SitePoint book ASAP:
http://php.net/tut.php
http://www.sitepoint.com/books/phpmysql4/

When you have a form script and an action script, it is often useful to combine them into one script that fulfills both requirements.  It helps keep things organized.  I also find that heredoc notation makes it easy to separate program logic from HTML presentation.

You might want to learn about "coding standards" and especially about indentation of the code.  I choose a modified Zend standard.  It helps keep my thinking on track by making my programming easy to read.
http://framework.zend.com/manual/en/coding-standard.html

If you have other specific questions about the scripts, please post back and I will try my best to answer. ~Ray

Author

Commented:
I am requestiing a little more help with this program please.
Most Valuable Expert 2011
Top Expert 2016

Commented:
I am requestiing a little more help with this program please.

Eh?  

I think you have received working examples showing exactly how to do what you asked about.  If you have a new question, please post a new question here at EE.  If you do not understand what was posted here, please tell us: What is your question?

Author

Commented:
I am a little confuse as to how that working example relates to my program and I would like an explanation. Please.
Most Valuable Expert 2011
Top Expert 2016

Commented:
It does what your program was trying to do.  It has the three parts that you were trying to get working: The HTML form, the PHP action script and the PHP display script.  The form and action script are packaged together into one script file (keeps things consolidated and organized).  The display script probably could be packaged into the same script file, but that seemed like more work, and it made sense to me to have a separate script.

HTML is generated by PHP programs.  The HTML is nothing more than a string of characters.  It follows certain rules for tags and content, but PHP does not care about those rules. To PHP, any HTML string is just a string variable. You can create string variables using HEREDOC notation.  It is a very powerful and easy-to-use form of string definition because it enables us to separate the creation of a web page (logic, external data, etc) from the display of the web page.  Professional programmers do not want logic embedded in the presentation layer for a variety of computer-science reasons that are too numerous to go into here.  But one good guideline to the quality of a programmer's work is a quick count of the number of times you find the <?php tags in the script.  Fewer is better.  One is best.

More on string definitions:
http://php.net/manual/en/language.types.string.php