Welcome to PHPMule.com
A blog site which I am posting to during my learning of the popular programming language PHP. Feel free to leave comments!
-
Making A Comments Form (Part 2)
Now we have created our comments form (See here: Making A Comments Form) we now need to add a bit of code to the PHP so it can be stored and retrieved later on.
You can store data in two ways:
By using a flatfile;
or using a database.
As i’m learning PHP I ain’t sure which ones best for your needs but having a database is best to store mass amounts of comments where as a flat-file is best for storing less amounts of data and obviously depends on what you need it for. The following file I have modified so any comments my visitors made could be retrieved later on by opening up the comments.txt file.
<?php
$firstname = $_POST['firstname'];
$emailaddress = $_POST['emailaddress'];
$comments = $_POST['comments'];
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$date = date(’H:i, jS F Y’);
?><html>
<head>
<title>Process Comment</title>
</head><body>
<?php
echo “<p>Following comment sent: </p><br />”;
echo “First Name: ” . $firstname . “<br />”;
echo “Email Address: ” . $emailaddress . “<br />”;
echo “Comments: ” . $comments . “<br />”;
$outputstring = $date.”\t”.$firstname.”\t”.$emailaddress.”\t”.$comments.”\n”;//File will be opened and be appended to
@ $fp = fopen(”$DOCUMENT_ROOT/../comments.txt”, ‘ab’);
flock($fp, LOCK_EX);
if (!$fp) {
echo “<p><strong>Your comment can’t be processed at the moment, please try submitting again later</strong></p></body></html>”;
exit;}
fwrite($fp, $outputstring, strlen($outputstring));
flock($fp, LOCK_UN);
fclose($fp);
echo “<p>Comment submitted.</p>”
?>
</body>
</html>
You will notice the new variable: $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
You will need this line as it tells the script where the source of the file will be when you write to it.
Another new line I have added is: @ $fp = fopen(”$DOCUMENT_ROOT/../comments.txt”, ‘ab’);
This line is created so you don’t have to create the file path of the whereabouts of the file. This will be used again… The ‘@’ symbol tells PHP to supress any errors so you don’t get those horrible messages! The ‘ab’ at the end means append to the file if your wandering.
The next line: flock($fp, LOCK_EX);
This line is used so it disables other visitors from posting a comment at the same time by locking the file. I think there is more of a practical way of handling files later but I haven’t learnt how to do that just yet..
You will then notice these lines in the script:
if (!$fp) {
echo “<p><strong>Your comment can’t be processed at the moment, please try submitting again later</strong></p></body></html>”;
exit;What this does is that if an error is found when sending the comment, it produces that error message and then exits the script.
The next line:
fwrite($fp, $outputstring, strlen($outputstring));The fwrite means to write to the file obviously. You will have come across the $fp variable (the file location), the $outputstring has all the variables such as $emailaddress which are sent to the file. The third bit of this line ’strlen($outputstring)’ avoids cross-platform issues when sending the comment basically.
After the comment is sent, it is required to unlock the file so that another visitor can send a comment by using this line: flock($fp, LOCK_UN)
and then close the file itself by using this line: fclose($fp);
It’s a complex process I know (the whole comment thing actually) probably because I’m a beginner at this stuff so I would recommend looking around the web to find a better way of learning the process (which is what I did!)
(Post created on Tuesday, April 7th 09 at 14:55)
-
PHP - Dynamic Content
Dynamic content in PHP basically means that the content changes. For example: If a visitor to your site submitted a comment you could have your script to output the date and time of the comment back to the user. As the date and time will be different in every comment it’s known as dynamic content. Let’s use the date function:
<?php
echo “<p>Comment processed. Date and time processed: “;
echo date (’H:i, jS F Y’);
echo “</p>”;
?>
The above will output to the browser: Comment processed. Date and time processed: 18:20, 4th April 2009
Quite easy to be honest and PHP can be used in many ways like this!
(Post created on Saturday, April 4th 09 at 18:40)
-
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Jun | ||||||
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | |||
Archives
Recent Articles
- PHP - Rounding Off Numbers
- PHP - Single vs Double Quotation Marks
- PHP - Loading Files Into Your PHP Script By Using require() and include()
- PHP - Replacing Strings With Other Strings
- PHP - Finding Strings Within Strings
- PHP - Using strlen() To Test String Length
- PHP - Using The explode() Function
- PHP - Changing The String Case
- PHP - The nl2br() Function
- PHP - Trimming Strings
Links
Categories
- PHP (22)
- Uncategorized (1)


