Jump to content

Recommended Posts

I have a craiglist like site to build. I want to add a feature where employer and employee can chat (1:1, very simple chat) if they are online, or post in private forum(1:1) if offline.

Any code out there? The client is paying too low for me to build things myself.

 

I found code but they are full featured forum and chat rooms/servers, I just need a plain and simple solution.

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/106407-need-11-forum-and-11-chat/
Share on other sites

That was fast... anyway here is the code.

 

request.php

<?php
@session_start();
include_once("chat.inc.php");

$mychat=new Chat($_REQUEST['room']);

if(!empty($_POST[msg]))
	$mychat->putMessage(trim($_POST[msg]),$_SESSION['SES_USER_NAME']);
if(isset($_POST['Logout']))
{
	$mychat->putMessage($_SESSION['SES_USER_NAME']." has loged out at ".date("h:i:s a"));
	$mychat->delUser($_SESSION['SES_USER_NAME']);
	?>
	<SCRIPT LANGUAGE="JavaScript">
	<!--
		window.parent.close();
	-->
	</SCRIPT>
	<?
}
?>
<HTML>
<body topmargin=0>
<form name='form1' method='Post' action=''>
<INPUT TYPE="hidden" name='room' value="<?=$_REQUEST['room']?>">
<input type="text" name="msg" size=75 >
<input type="Submit" value="Send">
<input type="Submit" name="Logout" value='Bye!'>
<br>
<span style='font-family:verdana;font-size:10px;color:#CC0000'>*Kindly use the bye button to close properly. It is recommended.</span>
</form>
<SCRIPT LANGUAGE="JavaScript">
<!--
document.form1.msg.focus();
//-->
</SCRIPT>
</body>
</HTML>

 

message.php

<?php
@session_start();
include_once("chat.inc.php");
$room=$_REQUEST['room'];
$mychat=new Chat($room);

?>
<HTML>
<head>
<!-- <meta http-equiv="refresh" content="1">-->

<STYLE>
body,td {
font-family:verdana;
font-size:11px;
}
</STYLE>


<SCRIPT LANGUAGE="JavaScript">
<!--
function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
this.obj = document.all[name];
this.style = document.all[name].style;
  }
  else if (document.layers)
  {
this.obj = getObjNN4(document,name);
this.style = this.obj;
  }
}

function getObjNN4(obj,name)
{
var x = obj.layers;
var foundLayer;
for (var i=0;i<x.length;i++)
{
	if (x[i].id == name)
	 	foundLayer = x[i];
	else if (x[i].layers.length)
		var tmp = getObjNN4(x[i],name);
	if (tmp) foundLayer = tmp;
}
return foundLayer;
}


//-->
</SCRIPT>

<script type="text/javascript" >
var g_remoteServer = "getmessage.php?room=<?=$room?>";
var g_intervalID;
function callServer() 
{	
	var head = document.getElementsByTagName('head').item(0);
	var old  = document.getElementById('lastLoadedCmds');
	if (old) head.removeChild(old);
	script = document.createElement('script');
	script.src = g_remoteServer;
	script.type = 'text/javascript';
	script.defer = true;
	script.id = 'lastLoadedCmds';
	void(head.appendChild(script));
}
g_intervalID = setInterval(callServer,1000);
callServer();
</script>

</head>
<body >
<div id='msgbox' style="position:absolute;left:10;border:thin inset;width:550;height:90%;overflow:auto;overflow-x:hidden;"><?=$mychat->getMessage();?>
</div>
<div id='userbox' style="position:absolute;left:560;border:thin inset;width:200;height:90%;overflow:auto">
<?=$mychat->getUsers();?>
</div>
</body>
<SCRIPT LANGUAGE="JavaScript">
<!--
var mydiv=new getObj('msgbox');
//alert(mydiv.obj.innerHeight);
mydiv.obj.scrollTop=10000;
mydiv.obj.scrollTop=10000;
//-->
</SCRIPT>
</HTML>

 

index.php

<?php
@session_start();
include_once("chat.inc.php");

if(!empty($_POST['name']))
{
$room="newroom";
$_SESSION['SES_USER_NAME']=$_POST['name'];
$mychat=new Chat($room);
if($mychat->addUser($_POST['name'])===false)
{
	$_SESSION['SES_USER_NAME']='';
	$_SESSION['MSG_LINES']=0;
	echo $mychat->room->getError();
}

}

?>
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--
function openchat()
{
window.open("chat.php?room=<?=$room?>",'','toolbar=0,status=0,height=500,width=770');
}
//-->
</SCRIPT>
</head>
<body>
<table cellspacning=1 cellpadding=2 border=0>
<?php if(empty($_SESSION['SES_USER_NAME'])){?>

<form name='' action='' method='POST'>
<tr><td><b>Enter Your Name:</b></td>
<td><input name='name'></td>
<td colspan=><input type='submit' name='addme' value='Go!'></td>
</tr></form>
<?php }else {?>
<tr><td>
<a href='javascript:openchat()'>click here</a> to start the <a href='javascript:openchat()'>Chat.</a>
</td></tr>
<SCRIPT LANGUAGE="JavaScript">
<!--
openchat();
//-->
</SCRIPT>
<?php }?>

</table>
</body>
</html>

 

getmessage.php

<?php
@session_start();
include_once("chat.inc.php");
$mychat=new Chat($_REQUEST['room']);

//$message=$mychat->getMessageForUser($_SESSION['SES_USER_NAME']);
$message=$mychat->getMessage();
$users=$mychat->getUsers();
if(!empty($message))
{
?>
var mydiv=new getObj('msgbox');
mydiv.obj.innerHTML="<?=$message?>";
var userbox=new getObj('userbox');
userbox.obj.innerHTML="<?=$users?>";

mydiv.obj.scrollTop=10000;
mydiv.obj.scrollTop=10000;
<?php }?>

 

chat.php

<FRAMESET ROWS="*,60" BORDER=0 name='main'>
<FRAME SRC="message.php?room=<?=$_REQUEST['room']?>" name="message" BORDER=0>
<FRAME SRC="request.php?room=<?=$_REQUEST['room']?>" name="request">
</FRAMESET>

 

chat.inc.php

<?php

class Chat
{
	var $room;
	var $filename;

	function Chat($roomName="Nay Help Line",$maxuser=
	{
		$this->filename=$roomName;
		if(empty($roomName))
		{
			$this->filename=time();
			$roomName=$this->filename;
		}
		$this->addRoom($roomName,$maxuser);
	}

	function putMessage($msg,$userName="")
	{
		$this->room->putMessage($msg,$userName);
		$this->_savechat();
	}

	function getMessage($fromline=0,$asArray=false)
	{
		return $this->room->getMessage($fromline,$asArray);
	}

	function getMessageForUser($userName,$asArray=false)
	{
		$message = $this->room->getMessageForUser($userName,$asArray);	
		$this->_savechat();
		return $message; 
	}

	function getRoomName()
	{
		return $this->filename;
	}

	function addRoom($roomName,$maxuser=
	{
		if(!$this->_is_room_exists($roomName))
		{
			$this->room=new ChatRoom($roomName,$maxuser);
			$this->_savechat();
		}
		else
			$this->room=$this->_getroom();
	}

	function addUser($userName)
	{
		if($ret=$this->room->addUser($userName)===false)
			return false;
		$this->_savechat();
	}

	function delUser($userName)
	{
		$this->room->delUser($userName);
		if($this->room->isRoomEmpty())
			$this->_removechat();
		else
			$this->_savechat();
	}

	function getUsers()
	{
		return $this->room->getUsers();
	}

	function close()
	{
		unset($this);
	}

	/* functions for internal uses */ 

	function _savechat()
	{
		$fp=@fopen($this->filename,'w');
		@fwrite($fp,serialize($this->room));
		@fclose($fp);
	}

	function _getroom()
	{
		return unserialize(file_get_contents($this->filename));
	}

	function _is_room_exists($room)
	{
		return is_file($room);
	}

	function _removechat()
	{
		@unlink($this->filename);
	}
}
?>

<?php

class ChatRoom
{
	var $name;
	var $maxuser;
	var $users;
	var $error;
	var $message;
	var $maxline;
	var $remlines;
	var $onefromip;
	var $mymessages;

	function ChatRoom($name="",$maxuser=
	{
		$this->name=$name;
		$this->maxuser=$maxuser;
		$this->maxline=2000;
		$this->remlines=1;
		$this->onefromip=false;
	}

	function OnlyOneUserFromIP($onefromip=false)
	{
		$this->onefromip=$onefromip;
	}

	function expandRoom($len)
	{
		$this->maxuser+=$len;
	}

	function shrinkRoom($len)
	{
		$this->maxuser-=$len;
	}

	function setWelcomeMessage($msg)
	{
		$this->mymessages['welcome']=$msg;
	}

	function setByeMessage($msg)
	{
		$this->mymessages['bye']=$msg;
	}

	function setMaxUser($maxuser)
	{
		$this->maxuser=$maxuser;
	}

	function setMaxMessageLine($lines=2000)
	{
		$this->maxline=$lines;
	}

	function setRemLine($lines=1)
	{
		$this->remlines=$lines;
	}

	function addUser($userName)
	{
		if(empty($userName))
		{
			$this->error="You must enter a name!";
			return false;
		}
		elseif(count($this->users) == $this->maxuser && $this->maxuser>0)
		{
			$this->error="I'm sorry, this session is full.";
			return false;
		}
		elseif(@array_key_exists($userName,$this->users))
		{
			$this->error=$userName." is already in room. Please use a different name if this is not you.";
			return false;
		}
		elseif($this->onefromip)
		{
			$user=$this->getUserByIP($_SERVER['REMOTE_ADDR']);
			if($user!=false)
			{
				$this->error=$user." is already in room from this IP address.";
				return false;
			}
		}
		$user=new User($userName);
		$this->users[$userName]=$user;
		if(!empty($this->mymessages['welcome']))
			$this->putMessage($this->mymessages['welcome']);
	}

	function delUser($userName)
	{
		$user=$this->users[$userName];
		$idx=@array_search($userName,@array_keys($this->users));
		@array_splice($this->users,$idx,1);
		if(!empty($this->mymessages['bye']))
			$this->putMessage($this->mymessages['bye']);

	}

	function isRoomEmpty()
	{
		return empty($this->users);
	}

	function putMessage($msg,$userName="")
	{
		if(count($this->message) > $this->maxline && $this->maxline>0)
		{
			for($i=1;$i<=$this->remlines;$i++)
			{
				@array_shift($this->message);
			}
			foreach($this->users as $user)
			{
				$user->lastline-=$this->remlines;
				if($user->lastline<0)
					$user->lastline=0;
			}
		}

		if(!empty($userName) && !empty($msg))
			$this->message[]="<b>".$userName.": </b>".$msg;
		elseif(!empty($msg))
			$this->message[]=$msg;

		/*$user=&$this->users[$userName];
		$user->lastline++;*/
	}

	function getMessageForUser($userName,$asArray=false)
	{
		$user=&$this->users[$userName];
		$messages=$this->getMessage($user->lastline,$asArray);
			$user->lastline=count($this->message);
			return $messages; 
	}

	function getMessage($fromline=0,$asArray=false)
	{
		$return="";
		for($i=$fromline;$i<count($this->message);$i++)
		{
			if($asArray)
				$return[]=$this->message[$i];
			else
				$return.=$this->message[$i]."<br>";
		}
		return $return;
	}

	function getError()
	{
		return $this->error;
	}

	function resetRoom()
	{
		$this->makeEmpty();	
		$this->name="";
		$this->maxuser=8;
		$this->maxline=2000;
		$this->remlines=1;
	}

	function setRoomName($name)
	{
		$this->name=name;
	}

	function getRoomName()
	{
		return $this->name;
	}

	function getUsers($asArray=false)
	{
		if($asArray)
			return @array_keys($this->users);
		else
			return @implode("<br>",@array_keys($this->users));
	}

	function getUser($userName)
	{
		return $this->users[$userName];
	}

	function getUserByIP($ip,$onlyName=false)
	{
		if(!is_array($this->users))
		{
			$this->error="No user is exists into room.";
			return false;
		}

		foreach($this->users as $user)
		{
			if($user->ip==$ip)
			{
				if($onlyName)
					return $user->name;
				else
					return $user;
			}
		}

		$this->error="No user found with this IP (".$ip.")";
		return false;
	}

	function makeEmpty()
	{
		unset($this->users);
		unset($this->message);
	}
}
?>

<?php

/* defining the diffrent status */

define("OFFLINE",0); 
define("ONLINE",1); 

class User
{
	var $name;
	var $ip;
	var $status;
	var $lastline;

	function User($user='')
	{
		$this->setUser($user);
		$this->ip = $_SERVER['REMOTE_ADDR'];
		$this->status = ONLINE;
		$this->lastline=0;
	}

	function setUser($name)
	{
		$this->name=$name;	
	} 

	function setStatus($status)
	{
		$this->status=$status;
	}

	function getUser()
	{
		return $this->name;
	}

	function getStatus()
	{
		return $this->status;
	}

	function getIP()
	{
		return $this->ip;
	}
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.