HTML and PHP Resources

Short easy to follow tutorials & more…

Simple PHP Flat File Shoutbox

| 9 Comments

About The Script

This is a simple PHP Shout Box or Guest Book script. All the information gathered by the script is stored in a text file as comma separated values.

Download the Script Files Now

Simple PHP Shout Box script. Includes all HTML, CSS, and PHP files.
Title : Flat File Shout Box Script
Caption : Flat File Shout Box Script
File name : com.htmlandphp.shoutbox.1.0.1.zip
Size : 3 kB

Further Explanation

/index.php

Scripts main File, It is used to display the posted messages and display the form used to post messages.

PHP
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/* Configuration */
$dataFile = './data/data.csv';
/* Required Files */
require_once './process/write.php';
require_once './process/read.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Shout Box | Guest Book Simple Script</title>
<link rel="stylesheet" type="text/css" href="/styles/styles.css">
</head>
<body>
<?php
if ($error !== false):
?>
<div id="error"><?php echo $error; ?></div>
<?php
endif;
?>
<div id="listing">
<?php
if (!empty($contents)) {
foreach ($posts as $post):
?>
<p class="post">
<b><?php echo $post['postingUser']; ?> </b>
(@<?php echo $post['postingTime']; ?>)<br>
<?php echo $post['postedMessage']; ?>
</p>
<?php
endforeach;
} else {
echo 'Nothing has been Posted. Be the first to post!';
}
?>
</div>
<div id="form">
<form action="<?php echo basename(__FILE__); ?>" method="post">
<label for="posting_user">Username</label>
<input type="text" name="posting_user" size="20" <?php if (isset($_POST['posting_user']))
echo 'value="' . $_POST['posting_user'] . '"';
?>>
<label for="posting_site">Site (Without http://)</label>
<input type="text" name="posting_site" size="20" <?php if (isset($_POST['posting_user']))
echo 'value="' . $_POST['posting_site'] . '"';
?>>
<label for="posting_message">Message</label>
<textarea name="posting_message" rows="5" cols="20"> <?php if (isset($_POST['posting_user']))
echo $_POST['posting_message'];
?></textarea>
<input type="submit" name="submit" value="Post!">
</form>
</div>
<a href="http://www.htmlandphp.com" title="HTML and PHP Resources" style="font-size: .5em;color:#999999;text-decoration:none;">Shout-Box Script From HTMLandPHP.com</a>
</body>
</html>

/process/write.php

This file is used to write new post to the data file. The file is loaded every time the script is called. When loaded the script checks to see if the form has been posted. If the form has not been posted it does nothing, if the form has been posted, then it processes the input, trimming extra white space, converting commas to their special character code and converting html entities to their html code equivalents.

PHP
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
31
32
33
<?php
$error = false;
if (isset($_POST['posting_user'])) {
$postUser = trim($_POST['posting_user']);
$postURL = trim($_POST['posting_site']);
$postMessage = trim($_POST['posting_message']);
if (($postUser != '') AND ($postMessage != '')) {
$postUser = str_replace(',', '&#44;', htmlentities($postUser));
if (!empty($postURL)) {
$postingUser = '<a href="http://' . urlencode($postURL) . '" target="_blank">' . $postUser . '</a>';
} else {
$postingUser = $postUser;
}
$postingTime = time();
$postedMessage = str_replace(array(','), '&#44;', htmlentities($postMessage));
$line = $postingUser . ',' . $postingTime . ',' . $postedMessage ."\n";
$fileHandle = fopen($dataFile, 'a');
if (!fwrite($fileHandle, $line)) {
$error = 'Could not write to file, try again.';
}
fclose($fileHandle);
// Delete post data so that fields do no populate again
unset($_POST);
} else {
$error = 'Username and message are required!';
}
}

/process/read.php

The read.php is used to read information from the file specified in index.php. If the file does not exist or if the file is empty, it displays an appropriate message. If the file is not empty, the script processes the file, line by line, and splits the data at every comma. The data stored in the data file is username, followed by a unix time stamp and the message in the third position.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
/*
* Read the data stored in $dataFile if it exists!
*/
$contents = file_exists($dataFile) ? file_get_contents($dataFile) : '';
// If the file does not exits then there is no need to break up any information
if (!empty($contents)) {
$lines = explode("\n", $contents);
$posts = array();
foreach ($lines as $line) {
$parts = explode(',', $line);
// Check to see if the line was more than a single element.
if (count($parts) > 1) {
$posts[] = array('postingUser' => $parts[0],
'postingTime' => date('m/d/Y H:m', $parts[1]),
'postedMessage' => $parts[2]);
}
}
}

/styles/styles.css

The styles.css is used to control the look of the page and layout of elements. It is a plain CSS file.

CSS
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
html,
body {
font-family: sans-serif;
font-size: 12px;
}
label {
float: left;
clear: both;
font-weight: bolder;
color: #ffffff;
}
input,
textarea {
border: solid 1px #343434;
float: left;
clear: both;
width:200px;
}
div#error {
padding:5px;
font-weight: bolder;
color: #FFF;
background-color: #FF8888;
width: 210px;
border:solid 1px #FF0000;
}
div#listing{
height: 250px;
width: 210px;
overflow: auto;
}
p.post {
font-size: 1em;
padding:5px;
border:solid #AAA 1px;
}
div#form {
background-color: #AAA;
overflow:auto;
padding:5px;
width: 210px;
}

9 Comments

  1. Hi

    Nice little script – i have found a small bug in the write.php:

    $postingUser = ‘‘ . $postUser . ‘‘;
    } else {
    $postingUser = $postURL;
    }

    this bit:

    $postingUser = $postURL;

    should read

    $postingUser = $postUser;

    to give a name output without the website being input.

    regards
    Dennis

    • Thanks for catching that. I updated the code. :-) I also updated read.php to ignore lines that don’t have enough elements after being split, like blank lines(this was generating a notice).

  2. another small code change you may wish to implement:

    open write.php

    find around line 12:

    a href=”http://’ . urlencode($postURL) . ‘”

    change to:

    a href=”http://’ . urlencode($postURL) . ‘” target=”_blank”

    the dane link will now open in a new window rather than the same one closing the shoutbox

  3. hi – yeah its me again

    am having a small difficulty getting the output to read from the bottom of the scroller where the latest enrty is
    or have the top of the scroller hold the latest entry

    hope you can help on this one

    thanks

    • In read.php look for the line that says: $lines = explode(“\n”, $contents); (should be line 10)
      and right after that add: $lines = array_reverse($lines);

      This reverses the order of the array.

  4. cheers for that – worked a treat

    ok another fix:

    index.php

    line 52: remove space between the > and < – makes 2 spaces in the message box when ppl go to post

  5. Hi

    i have been testing the shoutbox on a test page before i make it live and found that if i add a carriage return in the message input it adds the second line of text to a new output line on the top panel

    this is how it came out on the shout box once posted:

    This is the CArriage Return… (@01/01/1970 00:01)

    Dennis (Tech Admin) (@01/02/2012 07:02) Test Example

    any ideas how to fix this ?

    regards

    • Hey Dennis,

      Yesterday I updated read.php, theres an extra if inserted. Refer to the code above, look for the additional code around line 15:

      // Check to see if the line was more than a single element.
      if (count($parts) > 1) {....}

Leave a Reply

Required fields are marked *.

*