• Welcome to The Cave of Dragonflies forums, where the smallest bugs live alongside the strongest dragons.

    Guests are not able to post messages or even read certain areas of the forums. Now, that's boring, don't you think? Registration, on the other hand, is simple, completely free of charge, and does not require you to give out any personal information at all. As soon as you register, you can take part in some of the happy fun things at the forums such as posting messages, voting in polls, sending private messages to people and being told that this is where we drink tea and eat cod.

    Of course I'm not forcing you to do anything if you don't want to, but seriously, what have you got to lose? Five seconds of your life?

Guestbook Problem

Lorem Ipsum

New member
Age
24
Website
violetmist.aoriohoshi.com
I've coded a pretty simple PHP guestbook, which can be found here, and there is one minor error with it.

It doesn't particularly matter, but for organisation, it is good to have. The fopen function is in mode 'a+', which makes the posts go from first ever to most recent. But what I ideally want is the most recent at the very top of the page. I have tried to use the mode r+, but that just deletes some of the text in the guestbook.txt file it has been written to and leaves double <hr>s, random brackets and random letters all over the place.

What I'm asking is if there is any way in which you can protect the text which has already been written to guestbook.txt, and make it space forward if I use the mode r+, so I can get the most recent post first?
 

Lorem Ipsum

New member
Age
24
Website
violetmist.aoriohoshi.com
PHP:
<?php
if(isset($_POST["username"])){
$file=fopen("guestbook.txt", "a");
$date=date("d-m-Y");
$stringdata = "<p><b>" . $_POST["username"] . " says:</b><br>" . $_POST["message"] . "<br><div class='rightalign'><i>Posted on " . $date . "</i></div></p><hr>";
fwrite($file, $stringdata);
fclose($file);
}


else {
$file=fopen("guestbook.txt", "r+");
fclose($file);
}

?>
<?php include("guestbook.txt"); ?>
I presume that I'm allowed to post in response to this..?

That's basically the contents of the 'content' DIV on the receiving page of the guestbook form.
 

Butterfree

Still loves Joltik, though!
Staff member
Age
30
Website
www.dragonflycave.com
Pronoun
she/her
It would be easier to just read the guestbook file, explode it into an array with one post per array item and then reverse the array...

(Also, it is ridiculously easy to compromise security with this. People could theoretically type whatever PHP code they want into the guestbook and it would execute on the page.)
 

Eevee

usually right
Website
veekun.com
I've coded a pretty simple PHP guestbook
fuck PHP

What I'm asking is if there is any way in which you can protect the text which has already been written to guestbook.txt, and make it space forward if I use the mode r+, so I can get the most recent post first?
Search the manual for 'flock'; short for 'file-lock', and lets you grab an exclusive lock on a file. Open the file, lock it, do whatever (presumably add something to the front and write it all out again), then unlock and close it. While you have it locked, anything else trying to open the file will wait until you're done.
 
Top