<? 
    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($history, chr(13)) > 200) { 
            $history = substr($history, 1+strpos($history, chr(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>"; 
?> 
 
 |