Super-Mini Chat Room

Description: A super small, super simple chat room. No security, so use at your own risk.

<?PHP
 
// Contains db constants
REQUIRE "cst.inc";
 
/*
CREATE TABLE MINICHAT (
  NB tinyint(4) NOT NULL auto_increment,
  LOGIN varchar(20) NOT NULL default '',
  MESSAGE varchar(255) NOT NULL default '',
  ITSTIME varchar(10) NOT NULL default '',
) TYPE=MyISAM;
*/
 
// Number of messages to print
DEFINE( "_NB_MSGS_", 10 );
 
// Connect
MYSQL_CONNECT( $db_hostname, $db_username, $db_password );
MYSQL_SELECTDB( $db_database );
 
// Add message into minichat
FUNCTION addMessage( $login, $message ) {
 
   @SETCOOKIE( "minichatlogin", STRIP_TAGS( $login ) );
 
   $login = $_COOKIE['minichatlogin'] ? $_COOKIE['minichatlogin'] :
      MYSQL_ESCAPE_STRING( STRIP_TAGS( $login ) );
   $message = MYSQL_ESCAPE_STRING( STRIP_TAGS( $message, '<a><b><i><u>') );
   MYSQL_QUERY( "INSERT INTO MINICHAT ( NB, ITSTIME, LOGIN, MESSAGE ) VALUES ( "
   ._NB_MSGS_.", '".
     TIME()."', '".$login."','".$message."' )" );
   MYSQL_QUERY( "UPDATE MINICHAT SET NB=NB-1" );
   MYSQL_QUERY( "DELETE FROM MINICHAT WHERE NB < 1" );
}
 
// Returns messages
FUNCTION getMessages() {
   $rs = MYSQL_QUERY( "SELECT * FROM MINICHAT ORDER BY NB" );
 
   $ret = ARRAY();
   WHILE ( $msg = MYSQL_FETCH_ARRAY( $rs )) {
      $ret[] = DATE( 'h:m', $msg['ITSTIME'] )." ".$msg['LOGIN']." >".$msg['MESSAGE'];
   }
 
   RETURN $ret;
}
 
// Prints mini chat
FUNCTION miniChat() {
 
   $msgs = getMessages();
   @RESET( $msgs );
 
   ECHO '<form method="post">
         <table border="0" bgcolor="#000000" cellpadding="1">
         <tr><td>
         <table border="0" bgcolor="#ffffff" cellpadding="1">
   ';
 
   WHILE ( LIST(,$msg) = EACH( $msgs ))
      ECHO "<tr><td>$msg</td></tr>";
 
   IF ( !$_COOKIE['minichatlogin'] ) {
      IF ( !$_POST['login'] )
         ECHO '<tr><td>Login:<input type="text" name="login" size="6"></td></tr>';
      ELSE
         ECHO '<input type="hidden" name="login" value="'.$_POST['login'].'">';
   }
   ECHO '
   <tr><td><input type="text" name="msg" size="10"></td></tr>
   <tr><td align="center"><input type="submit" value="Send"></td></tr>
   </table></tr></td></table></form>';
}
 
 
// Message Posted ?
IF ( ISSET( $_POST['msg'] )) {
   addMessage( $_POST['login'], $_POST['msg'] );
}
?>
<html>
<head>
<title>Mini Chat Sample</title>
</head>
<body>
<center>
&LT;?PHP miniChat(); ?>
<hr />
</center>
</body>
</html>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.