Page 1 of 1

simple php, post page comments > 2 questions

Posted: Sun May 31, 2009 5:46 pm
by pearjam
Hi all!

I'm a linux server admin who's trying to teach myself php. I'm starting with trying to add a comment portion to pages on my site. I'm wanting to learn by hand, not grab someone else's script.

I've been working on something that seems simple, but there are two problems that I can't figure out - and I've been trying for about a week. :)

So, here goes:

The logic is to fill in a form with a name and a comment and hit submit. Php takes it and appends it to a flat file which is displayed with an include on the same page. I don't wish to have a "successful post" page. The idea is to be as trim and lightweight as possible. The php code is on the same html page as the form.

Problem number 1 is:
After the submit button is pressed, everything works fine - except you can hit refresh on the browser and it re-submits the same data even if nothings in the form fields. I've tried everything I can think of to clear the form, unset the variables etc... I'm not wanting to check the flat file for the same post (to keep processing low), and I don't want to use javascript.

How can I keep it from posting the same data once refresh on the browser's hit? (The direction I'd like to go is "if the field is empty, do not run" - I've tried 'if empty();' but I may not be doing it right - maybe it's not the right thing to do? Is there an html way to clear the form without javascript?


Problem number 2 is:
How can I display the last entries first with append? That may be to complex for what I'm trying to do, so I may not mess with that, but if there's a simple way - I'd like to at least explore it. I've tried 'rewind' with append to put the pointer back at the beginning of the file - but I may not be doing that right either... lol file_get_contents seems like it could take a lot of processing too - if the file gets big.


Note: I know the coding has bad form. Here's the code I have so far...

Code: Select all

 
<form method="get" action="<?php echo $_SERVER['SCRIPT_NAME']?>">
Who:<input type="text" name="who" style="width:100px;"><br />
What?<br /><textarea name="comment" style="width:100px;"></textarea><br />
<input type="submit" value="post"></form>
 
 
<?php
$user = $_GET["who"];
$message = $_GET["comment"];
$date = date("m|d|y");
if($user != ''){
$out = fopen("data.txt", "a+");
fwrite($out, $date. "&nbsp;<b>$user</b>&nbsp;said:<br />$message<br /><br />");
fclose($out); }
?>
 
<?php include('data.txt'); ?>
 
I would really appreciate any help or insight!!

Thank you!

Re: simple php, post page comments > 2 questions

Posted: Sun May 31, 2009 6:23 pm
by Griven
First, use the method POST for this sort of thing, not GET. GET puts all of the user's input data into the URL, which you generally don't want.

Second, you should divide this up into two pages, at least.

The first page is your form page. The second page, is the page that your form sends its POST data to. That second page will do all of your data validation, and will put the user's input wherever you want it to go (in this case, a flat file). After it's done validating and putting the data into the flat file, it redirects the user to wherever you want them to go, such as back to the comment page.

Here's some sample code:

comment.php

Code: Select all

<form method="post" action="commentactions.php">
Who: <input type="text" name="who" style="width:100px;" /><br />
What?<br /><textarea name="comment" style="width:100px;"></textarea><br />
<input type="submit" value="post" />
</form>
<?php include('data.txt'); ?>
commentactions.php

Code: Select all

$user = htmlentities($_POST["who"]);
$message = htmlentities($_POST["comment"]);
$date = date("m|d|y");
if ($user != '') {
$out = fopen("data.txt", "a+");
fwrite($out, $date. "&nbsp;<b>$user</b>&nbsp;said:<br />$message<br /><br />");
fclose($out); 
}
header('Location: comment.php');  //This will redirect your user
With that said, I have a question: Why not use a database to store your comments and such? It's much more flexible and powerful than using a flat file like you're doing right now.

Re: simple php, post page comments > 2 questions

Posted: Sun May 31, 2009 6:34 pm
by mikemike
Hi,

I do not agree with Griven's comments of having two files, there is absolutely nothing wrong with one file - I use the same kind of process on many of the projects I have worked on including websites for massive companies with thousands of hits daily with no problem - it's simply not an issue.

With regards to why the form is resubmitting, it's simple. When you refresh the page you're sending all of the headers again, included in these headers is the data you're sending. You have a few options you coudl use to stop re-submission:
- Refresh the page using PHP's header() function: header("Location: /path/to/file.php");, this will reload the page without the data headers so if the user then refreshed themselves nothing would be resubmitted.
- Use sessions/cookies to monitor the users posting
- Mark the timestamps in the file and ensure that the latest comment isn't the same and posted in the last 5minutes, for example

For your second point I must bring up one question: why are you not using a database like MySQL? You say you're a Linux admin so it should be pretty easy to set up. If you're concerned about processing ower then running a MySQL daemon is likely to use far less resources than openning a potentially huge comment file - it will also allow you to make more resource-friendly scripts.

If you absolutely must use a flat-file then you could load the file into an array line-by-line, flip it, and then loop through it. See the following functions:
- fgets()
- array_reverse()
- foreach loop

Re: simple php, post page comments > 2 questions

Posted: Mon Jun 01, 2009 7:05 am
by pearjam
Thank you both for your answers!

The reasons I'm not using a mysql db:

1. Because I'm writing it all by hand, there's no CMS, phpMyAdmin, or server control panel - I'm not even using a html editor. It's just me, gedit and a terminal. That means I would have to create the db and remove entries manually, which would be a pain. Removing entries from a flat file is easier.

2. It's another service to keep running, and another service that could fail and take down the site. There is no job or service with a flat file.

3. When the page is loaded, it must make that connection / session.

4. It's a possible point of compromise.

5. I'm not a dba, so writing it, optimizing it and securing it would be another thing to learn, while learning php.

6. When the code I'm learning to write fails, it's easier to troubleshoot one thing, than two.

7. Because the data is not sensitive, and is simple there's not much of a need for a powerful db.

8. Here's the biggest reason of all... One of the main goals of the site is to be as static as possible, with only a few dynamic elements (I wrote a simple php counter for it.).

So those are the reasons why I guess!




mikemike - I was hoping to take advantage of the submit buttons automatic refreshing. I'll try the header though and see how it does.

Clearing the variables out wouldn't do it? I'm assuming I'm not doing it right... lol :D