Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - Mini-Chat Tutorial
MISCELLANEOUS

Mini-Chat Tutorial
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 17
    2002-07-24

    Table of Contents:
  • Mini-Chat Tutorial
  • Basic Printing
  • Improving
  • Final Script

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Mini-Chat Tutorial - Final Script


    (Page 4 of 4 )

    <?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 '',
      PRIMARY KEY  (NB),
      UNIQUE KEY NB (NB),
      KEY NB_2 (NB)
    );
    */

    // 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 ( '10', '".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>';
    }
    ?>
    <html>
    <head>
    <title>Mini Chat Sample</title>
    </head>
    <body>
    <?php miniChat(); ?>
    </body>
    </html>

    Next time, we'll see how to ensure that no more than one person has the same login, which is possible right now, introduce smileys and a censor system, as well as an anti-spam system.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap