Login   Register  
PHP Classes
elePHPant
Icontem

File: chatbox.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Richard Keizer  >  Shared Memory  >  chatbox.php  >  Download  
File: chatbox.php
Role: Example script
Content type: text/plain
Description: Usage example
Class: Shared Memory
Store and retrieve information in shared memory
Author: By
Last change: bugfixed init
Date: 2008-11-27 10:18
Size: 977 bytes
 

Contents

Class file image Download
<?
    
include_once "sharedmemory.class.php";

    
$s = new SharedMemory(54645645);                    //create instance of shared memory object
    
$history $s->Get();                        //read contents
    
    
$username = isset($_POST["username"])?$_POST["username"]:"Anonymous";
    
    
//add user message to history and write result back to shared memory
    
if (isset($_POST["chat"]) && $_POST["chat"] != "") {
        
$history .= sprintf("%s says: %s"$username$_POST["chat"].chr(13));
        
//strip oldest message if history exceeds 200 lines
        
if (substr_count($historychr(13)) > 200) {
            
$history substr($history1+strpos($historychr(13)));
        }
        
$s->Set($history);
    }
    
    
//render history to textarea
    
echo "<textarea rows=30 cols=80>$history</textarea>";
    
    
//render user input controls
    
echo "<form method='POST'>";
    echo 
"  <input type='text' size=20 name='username' value='$username'>";
    echo 
"  <input type='text' size=40 name='chat'>";
    echo 
"  <input type='submit' value='Send/Refresh'>";
    echo 
"</form>";
?>