PDA

View Full Version : Functions



recore684
05-26-2005, 09:24 PM
The functions I am using for my guestbook are not working properly and I was wondering if someone could point me in the right direction.

<?php
// data grabbing here/
$fname = $_POST ['fname'];
$lname = $_POST ['lname'];
$gender = $_POST ['gender'];
$comment = $_POST ['comment'];
$submit = $_POST ['submit'];

function displayContents ($guestbookfile)
{
@$mf = fopen("../guestbook/guestbook.txt", "r");
if (!$mf)
{
$out = "<p class\"err\">Could not open the guestbook file for reading!</p>";
}
else
{
while (!feof($mf))
{
$entry = fgets($mf, 3000);
if (!feof($mf))
{
$data = explode("\t", $entry);
$out .= "<blockquote><span class=\"quote\">" . stripslashes($data [7]) ."</span><br />";
$out .= "<cite>-<span class=\"author\">$data[3] $data[4]</span><br /";
$out .= "A $data[5] $data[6]<br />";
$out .= "From $data [2]<br />";
$out .= "at $data[0] on $data[1]";
$out .= "</cite></blockquote>\n";
}
}
fclose($mf);
}
return $out;
}

function displayForm($fname, $lname, $gender, $comment, $err)
{
echo "err\n";
?>

<?php
}

function insertData($outstring, $guestbook)
{
@$mf = fopen($gbFile, "a");
if (!$mf)
{
$err = "<p class=\"err\">Could not open guestbook file for writing!</p>";
}
else
{
if (flock($mf, LOCK_EX))
{
fwrite($mf, $outstring);
flock($mf, LOCK_UN);
fclose($mf);
$err = "success!";
}
else
{
$err = "<p class=\"err\">Could not establish a file lock!</p>";
}
}
return $err;
}
function processData($fname, $lname, $gender, $comment)
{
$outstring = date('h:i a') . "\t" . date('j F, Y') . "\t";
$outstring .= $_SERVER['REMOTE_ADDR'] . "\t";
$outstring .= addslashes(trim($fname)) . "\t" . addslashes(trim($lname)) . "\t";
$outstring .= $gender . "\t";
$outstring .= addslashes(trim($comment)) . "\n";
$err = insertData($outstring, "../guestbook/guestbook.txt");
if ($err = "success!")
{
echo displayContents("../guestbook/guestbook.txt");
}
else
{
echo $err;
}

function valid($fname, $lname, $gender, $comment)
{
if (is_null($fname) || empty($fname) || $fname == "") $err .= "Please enter your first name<br />";
if (is_null($lname) || empty($lname) || $lname == "") $err .= "Please enter your last name<br />";
if ($gender != "male" && $gender != "female") $err .= "Please indicate your sex<br />";
if (is_null($comment) || empty($comment) || $comment == "") $err .= "Please leave me some comments<br />";
return $err;
}
?>

mwinter
05-26-2005, 10:46 PM
The functions I am using for my guestbook are not working properlyYet you fail to:


describe what "working properly" means. Code always does what it's supposed to, but not necessarily what you want it to.
indicate which function is causing problems.
show an example of usage.
provide any error messages, or even state if there were any error messages.
You wouldn't go to a doctor and just say, "Something's wrong." You'd provide information to help with the diagnosis. The same is true when reporting software bugs, and asking for help with problematic code.

A final note before continuing: when posting code, please use the
@$mf = fopen("../guestbook/guestbook.txt", "r");[/quote]This is perhaps a style issue more than anything else, but it seems to be traditional to place the error suppression operator on the right-hand side of assignment expressions:

[php]$mf = @fopen('../guestbook/guestbook.txt', 'r');



while (!feof($mf))
{
&#160;&#160;$entry = fgets($mf, 3000);
&#160;&#160;if (!feof($mf))Again, not errors, but two things to consider.

Do you actually check for input longer than 3000 characters when updating the guestbook? You don't appear to, and this could break your file format.

You don't need to check for EOF a second time. The fgets function will return false if it encounters EOF immediately, so


if($entry) {is sufficient.


$data = explode("\t", $entry);
$out .= "<blockquote> [...]";This is bad form (but not an error in PHP 5, at least) because you haven't defined out. You do this in the valid function, too.


$out .= "<cite> [...] <br /";You haven't included a closing angle bracket.


function insertData($outstring, $guestbook)
{
&#160;&#160;@$mf = fopen($gbFile, "a");The variable, gbFile, isn't defined. Again, this isn't an error, but it's still wrong.


function processData($fname, $lname, $gender, $comment)
{
&#160;&#160;$outstring = date('h:i a') . "\t" . date('j F, Y') . "\t";Most operations on dates should be using exactly the same time stamp. Call time, store its value, then pass it to the two date calls.


function valid($fname, $lname, $gender, $comment)
{
&#160;&#160;if (is_null($fname) || empty($fname) || $fname == "")You can probably replace all of that with:


if(empty($fname)) {Read the PHP documentation for the empty function (http://www.php.net/manual/en/function.empty.php).

If none of that helps, then you need to provide more information.

Mike