Write a comment on this article
In this tutorial we make a simple script to provide handy little tool.
The idea: visitors shall be able to write short comments (shouts) through a simple form. The most recent comments are shown below the form immediately. The visitor has the possibility to view all comments.
What do we need? Definitely, we need two files. One to store the comments (shouts.txt) and one to read and write the comments (index.php). It's very important that the file shouts.txt is writable by the HTTP server (adjust security on Windows system or use the chmod command on Unix systems).
Before we start the programming part, let's make a short concept:
Seems quite simple. Well, it is! First things first. Here is the HTML code for a simple form with two input fields (name and shout) and a submit button. Recognized the variable $_SERVER["PHP_SELF"]? This variable returns the path and name of the current file, so if you copy this code to another PHP file, the form will be submitted correctly.
<p>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<input type="text" value="Your name" name="input_name" /><br />
<input type="text" value="Your text" name="input_text" /><br />
<input type="submit" value="Shout!" />
</form>
</p>
Next step is to check if the form has been submitted and the form has been filled in with at least some useful data (we don't want empty names or shouts). In this case, we open the shouts file for writing with the function fopen, write the data (we write both the name and the shout in one write delimited with five pipes) with fputs and close the file with fclose.
//insert new shout
$input_name = $_POST["input_name"];
$input_text = $_POST["input_text"];
//check if form has been submitted
if(isset($input_name) && isset($input_text) && $input_name!="Your name" &&
$input_text!="Your text" && strlen($input_name)>0 && strlen($input_text)>0)
{
$handle = fopen("shouts.txt","a"); //open shouts file to write (append)
fputs($handle,"$input_name|||||$input_text\n"); //insert name and shout
fclose($handle); //close file handle
}
This leads us to the last point: read the shouts file and print its content. So we open the file again, but this time for reading only, read the data row by row, split name and comment, save the values in two arrays (names and shouts), close the file again, reverse the arrays so that the newest comments come first and print the shouts.
//read shouts file
$names = array(); //array to store names
$shouts = array(); //array to store shouts
$handle = fopen("shouts.txt","r"); //open shouts file to read
while(!feof($handle)){ //read row by row
$row = fgets($handle,999999);
list($name,$shout) = split("\|\|\|\|\|",$row);
if($name){
array_push($names,$name);
array_push($shouts,$shout);
}
}
fclose($handle); //close file handle
//reverse arrays so that new lines are first
$names = array_reverse($names);
$shouts = array_reverse($shouts);
//print shouts
for($i=0;$i<count($names);$i++){
?><p><strong><?php echo $names[$i]; ?>:</strong> <?php echo $shouts[$i]; ?></p>
<?php } ?>
That's it! Handy little tool, right?
You can try this script here: demos/Shoutbox/index.php. Or download it here: Shoutbox_1-0.zip
PHP, tutorial, shoutbox
If you have a question or a problem and you're looking for a solution, please use the forum. Comments might not be answered.