Version: 1.1

Type: Full Script

Category: BBS/Discussion

License: GNU General Public License

Description: A very basic web chat script that was based on the tutorial found on this site, It is designed to be very lightweight on both the server and network bandwidth. Will work on any Unix server. -- 7 files.



############## file 1 index.htm #################
<html>
<head>
<title>index</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<frameset cols="100,*" frameborder="NO" border="0" framespacing="0"> 
  <frame name="leftFrame" scrolling="NO" noresize src="left.php">
  <frameset rows="120,*" frameborder="NO" border="0" framespacing="0">
    <frame name="topFrame" scrolling="NO" noresize src="chat.php" >
    <frameset rows="5,*" frameborder="NO" border="0" framespacing="0" cols="*">
        <frame name="topFrame1" scrolling="NO" noresize src="refresh.php" >
        <frame name="mainFrame" src="messages.htm">
        </frameset>
  </frameset>
</frameset>
<noframes>
<body bgcolor="#FFFFFF" text="#000000">
This page requires a web browser with frames support
</body>
</noframes> 
</html>
############# end file1 index.htm ###########

############# file2 chat.php #############
<html>
<head>
<title>Chat</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">

<? 
// if name is not entered, present the form again
if(!$chatname) {
        print_form();
        exit;
        };
// do not post a message unless there is a message
if(!$message) {
        print_form_nameset();
        exit;
        };

//  set up timestamp for messages
$time = date("H:i d/m");

// read in messages file into an array
$message_array = file("messages.htm");

// get the last 10 messages for display
for ($counter = 1; $counter < 10; $counter++) {
        $old_messages .= $message_array[$counter];
}

// read in history file into an array
$history_array = file("history.htm");

// get the last 100 messages for display
for ($counter = 1; $counter < 500; $counter++) {
        $old_history .= $history_array[$counter];
}

// prepare the format of the new message

$chatname = htmlspecialchars($chatname);
$message = htmlspecialchars($message);
$new_message = "$time &lt&gt $chatname ::: $message<p>\n"; 

// set up some variables  -header and footer for the messages page
$header = "<html><head>".
        "<meta http-equiv=\"pragma\" content=\"no-cache\">".
        "<meta name=\"robots\" content=\"noindex\"></head>".
        "<body bgcolor=\"#000000\" text=\"#ffffff\"><p>\n";
        //"<meta http-equiv=\"refresh\" content=\"8\">".

$footer = "<hr width=\"90%\">".
        "<center>BWChat &copy 2002</center></body></html>";

// write the new message file
$open_file = fopen("messages.htm", "w");
fputs($open_file, $header);
fputs($open_file, stripslashes($new_message));
fputs($open_file, $old_messages);
fputs($open_file, $footer);
fclose($open_file);

// write the history file
$open_file = fopen("history.htm", "w");
fputs($open_file, $header);
fputs($open_file, stripslashes($new_message));
fputs($open_file, $old_history);
fputs($open_file, $footer);
fclose($open_file);


print_form_nameset();

function print_form()
{
global $chatname;
global $PHP_SELF;
echo <<<FORM1
<p>
<font size="-1">
<form name="chat" method="post" action="$PHP_SELF" >
ChatName : <input type="text" name="chatname" size="30" value="$chatname"><br>
Message : <input type="text" name="message" size="80"><br>
<input type="submit" value="Post Message"><br>
</form>
</font>
FORM1;
}

function print_form_nameset()
{
global $chatname;
global $PHP_SELF;
echo <<<FORM2
<p>
<font size="-1">
<form name="chat" method="post" action="$PHP_SELF" >
ChatName : $chatname<br> <input type="hidden" name="chatname" value="$chatname">
Message : <input type="text" name="message" size="80"><br>
<input type="submit" value="Post Message"><br>
</form>
</font>
FORM2;
}

?>
</body>
</html>
############### end file 2 chat.php ###############

############# file 3 refresh.php #############
<?
$path_parts = pathinfo("$PHP_SELF");
$tail_URL = $path_parts["dirname"];
$head_URL = $HTTP_SERVER_VARS["SERVER_NAME"];
$mybase = "http://$head_URL"."$tail_URL";

$lastmsg = fileatime("messages.htm");
$now = time(); 

if( $now -8 < $lastmsg) need_to_update_messages();

do_nothing();


function need_to_update_messages()
{
global $mybase;
echo <<<UPDATE
<html>
<head>
<meta http-equiv="refresh" content="8">
<meta http-equiv="pragma" content="no-cache">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<script language="javascript">
parent.frames["mainFrame"].location.href = "$mybase/messages.htm";
</script> 
 $mybase
</body>
</html>
UPDATE;
exit;
}

function do_nothing()
{
echo <<<NOUPDATE
<html>
<head>
<meta http-equiv="refresh" content="8">
<meta http-equiv="pragma" content="no-cache">
</head>
<body>
</body>
</html> 
NOUPDATE;
exit;
}

?>
####################### end of file 3 refresh.php #################

########## file 4 left.php ######################
<html>
<head>
<title>History</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<p>&nbsp;
<p>&nbsp;
<h2>
<a href="history.htm" target="_blank">History</a>
</h2>
<p>&nbsp;
</body>
</html>
################## end of file 4 left.php   ########

############## file 5 README ##################
#Readme for BWChaT
#
# BWChaT Author Bob PHILLIPS bob@norcom.net.au
# (c) 26th September, 2002
# Version 1.1
# 
# This software is distributed as freeware.  Please give me credit for this and any derived works.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND
#

This chat is based on the tutorial at http://www.phpbuilder.com/columns/mhall20000621.php3

Is is designed to be very lightweight, with minimal requirements for use.

You just need a UNIX-Based PHP enabled web server, and a web browser that supports JAVA.

All files are commented and should be easy to modify.

I do not intend to add any functionality, it is designed to be very simple, yet functional.

Files contained in the chat

README
chat.php
history.htm
index.htm
left.php
messages.htm
refresh.php

----------
INSTALL

Put the above files in a directory called BWChat/

Make sure the web server is able to write to the history.htm and messages.htm files

(touch history.htm messages.htm ; chmod 777 history.htm messages.htm)

point your web browser to this directory and it will work!

Bob
-------
###### end file 5 README ######################