Icontem

File: mostlc.php

Recommend this page to a friend!
  Classes of Dixan Santiesteban  >  Little Chat  >  mostlc.php  >  Download  
File: mostlc.php
Role: Auxiliary data
Content type: text/plain
Description: Php Script
Class: Little Chat
Display and send chat messages using AJAX
Author: By
Last change:
Date: 7 years ago
Size: 1,734 bytes
 

Contents

Class file image Download
<?
extract($_POST);
session_id('mostlittlechat');
session_start();

$_SESSION['lastmessage']=!isset($_SESSION['lastmessage'])?0:$_SESSION['lastmessage'];
$_SESSION['msgs']=!isset($_SESSION['msgs'])?array():$_SESSION['msgs'];
$_SESSION['users']=!isset($_SESSION['users'])?array():$_SESSION['users'];

switch($action){
case "login":
	$arr=$_SESSION['users'];
	foreach($arr as $us){
		if ($us==$user) die("alert('user in use');");
	}


	
	$listusers = implode(",", $_SESSION['users']);
	$last=$_SESSION['lastmessage'];
	$ret.="self.start('$user','$listusers');\r\n";
	array_push($_SESSION['users'],$user);
	addmess("self.enter('$user');");

case "ping":
	
	$_SESSION["$user.time"]=time();
	$arr=$_SESSION['users'];
	foreach($arr as $us){
		if ($_SESSION["$us.time"]<(time()-10)){
			removefromusers($us);
			addmess("self.remove('$us');\r\n");
		}
	}
	
	if(isset($message)){
		$message=htmlentities($message);
		addmess("self.message('$user','$message');");	
	}
	
	//$last++;
	while(intval($last)!=intval($_SESSION['lastmessage'])){
		$ret.=$_SESSION['msgs'][$last]."\r\n";
		$last++; if ($last>99) $last=0;
	}
	
	$ret.="$('#last').val($last);\r\n";
	
break;

}

echo $ret;


function removefromusers($who){
	$ret=array();
	$arr=$_SESSION['users'];
	foreach($arr as $us){
		if ($us!=$who) array_push($ret,$us);
	} $_SESSION['users']=$ret;
}


function addmess($msg){
	if (count($_SESSION['msgs'])<100) {
		array_push($_SESSION['msgs'],$msg);
		$_SESSION['lastmessage']=count($_SESSION['msgs']);
		
	} else {
		$_SESSION['lastmessage']++;
		if ($_SESSION['lastmessage']>99) $_SESSION['lastmessage']=0;
		$_SESSION['msgs'][$_SESSION['lastmessage']]=$msg;
	}
}

?>