WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : A simple php chat script that barely needs any bandwidth. Perfect for targeting low speed users.
Categories : PHP, Strings, Filesystem Click here to Update Your Picture
Shashank Prabhakara
Date : May 30th 2009
Grade : 1 of 5 (graded 1 times)
Viewed : 9474
File : 4968.zip
Images : No Images for this code example.
Search : More code by Shashank Prabhakara
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
Like this code?
Show the author your appreciation.
 

Simply copy chat.php's contents where the chat page is required and customise.

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>chat</title>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">

</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
<?
$person
= str_replace ("\n"," ", $person);
$person = str_replace ("<", " ", $person);
$person = str_replace (">", " ", $person);
$person = stripslashes ($person);
?>
<form action="chat.php3" method="post">
Nickname: <input type="text" name="person" size="40" maxlength="80" value="<? echo $person; ?>"><br>
Your message: <textarea name="message" rows="3" cols="40"></textarea>
<input type="submit" value="Send/refresh">
</form>

<?
/*    the $chat_file_ok is a txt file (or whatever else) I use for                */
/*    messages storage                                        */

$chat_file_ok = "c:\\apache\\htdocs\\cart\\chat\\msg.txt";

/*    $chat_lenght is the number of messages displayed                */

$chat_lenght = 10;

/*    $max_file_size is the maximum file size the msg.txt file can reach        */
/*    assuming that any chatter doesn't write a message longer than        */
/*    $max_single_msg_lenght (this case: 100,000 bytes = 100Kb)        */

$max_single_msg_lenght = 100000;
$max_file_size = $chat_lenght * $max_single_msg_lenght;

/* check if file size is over maximum    (set with $max_file_size )            */

$file_size= filesize($chat_file);

/*    if file size is more than allowed then                            */
/*            reads last $chat_lenght messages (last lines of msg.txt file)    */
/*            and stores them in $lines array                        */
/*            then deletes the "old" msg.txt  file and create a new msg.txt    */
/*            pushing the "old" messages stored in $lines array into the    */
/*            "new" msg.txt file using $msg_old.                       */
/*        Note: this is done in order to avoid huge msg.txt file size.        */
           
if ($file_size > $max_file_size) {

/* reads file and stores each line $lines' array elements    */

$lines = file($chat_file_ok);
/*get number of lines                                */

$a = count($lines);

$u = $a - $chat_lenght;
for(
$i = $a; $i >= $u ;$i--){
       
$msg_old $lines[$i] . $msg_old;
    }
$deleted = unlink($chat_file_ok);
$fp = fopen($chat_file_ok, "a+");
$fw = fwrite($fp, $msg_old);
fclose($fp);
}

/* the following is because every message has to be        */
/* placed into one single line in the msg.txt file.            */
/* You can render \n (new lines) with "<br>" html tag anyway.    */

$msg = str_replace ("\n"," ", $message);

/*    if the user writes something...                    */
/*        the new message is appended to the msg.txt file    */
/*    REMEMBER: the message is appended, hence, if     */
/*        you want the last message to be displayed as the    */
/*         first one, you have to                     */

/*        1. store the lines (messages) into the array        */
/*        2. read the array in reverse order                */
/*        3. post the messages in the output file (the chat)    */
       
/* I added these three lines in order to avoid buggy html code and slashes    */
$msg = str_replace ("\n"," ", $message);
$msg = str_replace ("<", " ", $msg);
$msg = str_replace (">", " ", $msg);
$msg = stripslashes ($msg);




if (
$msg != ""){

$fp = fopen($chat_file_ok, "a+");
$fw = fwrite($fp, "\n<b>$person :</b> $msg<br>");
fclose($fp);
}

$lines = file($chat_file_ok);
$a = count($lines);

$u = $a - $chat_lenght;

/*    reads the array in reverse order and outputs to chat    */
for($i = $a; $i >= $u ;$i--){
        echo
$lines[$i] . "<hr>";
    }


?>
</body>
</html>





Grab images from one or more URLs and save them to a specified local directory.
Categories : PHP, Filesystem, Strings, Arrays
Working with files - putting file contents to a string / var
Categories : PHP, Filesystem, Variables, Strings
Massreplace
Categories : Filesystem, Regexps, Strings, PHP
how can I read the entire contents of a file into a string?
Categories : Filesystem, Strings, PHP
Compare two texts and display a block of text with the differences between them.
Categories : PHP, PHP Classes, Filesystem, Strings, Arrays
How to ifconfig down/up a list of IP's
Categories : Arrays, Strings, Filesystem, PHP
Variable serialization and unserialization. Loading and saving variable structures to and from file.
Categories : Arrays, Filesystem, Variables, Strings, PHP
How to find the name of the current file?
Categories : PHP, Filesystem, Strings
Read a file with strings and create a new file with the first half of each string
Categories : PHP, Strings, Filesystem
Working with files - return an array of files within a directory
Categories : PHP, Strings, Variables, Filesystem
Simple Email address validation
Categories : Email, PHP, Strings
Look for the *position* of the first occurence of string2 in string1, beginning at position start.
Categories : Complete Programs, PHP, Strings
QTO File Manager
Categories : Filesystem, filePro, Complete Programs, PHP, Content Management
A function that parses a string and replaces http://whatever with a link, and email addresses with a mailto link. This function was designed for the motd package. But will work freely on its own.
Categories : PHP, Strings
Save and restore files into postgresql database (PHP SCRIPT) PHP CLASS
Categories : PHP, Databases, PostgreSQL, Filesystem