Click to See Complete Forum and Search --> : how many times?


derezzz
10-27-2003, 01:30 AM
Below is PHP for a guestbook that Pyro posted a little while ago. I am using it, but I am wondering, once I upload it to my site, is there anything I need to change in the code if I want to have multiple guestbooks in one folder? In other words, are the pages self contained or if put them under the same folder, will they try to load into each other even with a file name change?

<?PHP

$filename = "guestbook.txt"; #CHMOD to 666
$text = "";

if (isset($_POST["submit"])) {
$name = htmlspecialchars($_POST["name"]);
$message = htmlspecialchars($_POST["message"]);

$date = date ("l, F jS, Y");
$time = date ("h:i A");

$value = $message."<br>\n<span style=\"font-family: verdana, arial, sans-serif; font-size: 70%; font-weight: bold;\">Posted by $name at $time on $date.</span>\n<break>\n";

##Write the file##

$fp = fopen ($filename, "a");
if ($fp) {
fwrite ($fp, $value);
fclose ($fp);
}
else {
echo ("Your entry could not be added.");
}
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Guestbook</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="<?PHP echo $_SERVER["PHP_SELF"]; ?>" method="post">
<p>Your Message:<br>
<textarea name="message" rows="7" cols="50"></textarea><br>
Your Name: <input type="text" name="name">
<input type="submit" name="submit" value="submit"></p>
</form>

<?PHP

$contents = @file($filename) or die("No files in guestbook.");

foreach ($contents as $line_num => $line) {
$text .= $line;
}

$text = split("<break>", $text);

for ($i=0; $i<count($text)-1; $i++) {
echo "<div style=\"border: gray 1px solid; padding: 5px; font-family: arial, sans-serif; background-color: #eeeeee;\">";
echo $text[$i];
echo "</div>";
}

?>
</body>
</html>

the-FoX
10-27-2003, 03:53 AM
the guestbook data is stored in the file, defined in the first line. maybe you should add a prefix for every guestbook... so every guestbook got it's own data-file.

$filename=$id."filename.txt";

pyro
10-27-2003, 07:21 AM
Or, you could name the .txt file with the same name as the .php file, so you know which one corresponds to which PHP page.

Replace

$filename = "guestbook.txt"; #CHMOD to 666With

<?PHP
$fullname = basename($_SERVER['PHP_SELF']); #get the filename of the current file
preg_match("/(.*)(?=\\.)/", $fullname, $match); #match the file excluding the extention
$filename = $match[0].".txt"; #add a .txt extention
?>So, if you file is named foo.php, the .txt file with be foo.txt.

the-FoX
10-27-2003, 07:52 AM
but than you need for every guestbook one php file. right ?

pyro
10-27-2003, 08:03 AM
No, you need every guestbook in separate PHP files. That takes the filename of the PHP file, strips the .php, adds a .txt and saves data to that.