#native_company# #native_desc#
#native_cta#

Php Private Chat

By kakchan
on March 9, 2004

Version: 0.01

Type: Sample Code (HOWTO)

Category: Databases

License: GNU General Public License

Description: Hello this script is very usefull when you need unlimited chat on your web site, is very easy to install, create 4 file and put the follow code into them…
this script is fully open source you can modify it as you needed but notify me before.
remember to change db settings with your own settings

Database Dump:

#
# Table structure for table `chat_private`
#

CREATE TABLE `chat_private` (
`chat_date` int(10) default NULL,
`chat_psw` varchar(20) NOT NULL default ”,
`chat_msg` text NOT NULL
) TYPE=MyISAM;

// File index.php 

<html> 
<body bgcolor="#000000" text="#FFFFFF"> 
<div align="center"> 
  <p>   
    <form name="form1" method="post" action="chat_read.php"> 
    Password: <input name="password" type="text" id="password" maxlength=20> 
    Nickname: <input name="nickname" type="text" id="nickname" maxlength=20> 
    <input name="Entra" type="submit" id="Entra" value="Entra"> 
  </form></p> 
</html> 

// File chatlib.php 
<? 
// Db config 
$hostname = ""; 
$user = ""; 
$password = ""; 
?> 
<html><head><title>FORM</title> 
<script language='JavaScript'> 
<!-- 
function focus() { 
document.mesform.message.focus(); 
} 
function aggiorna() { 
document.chatroom.location.reload(); 
} 
--> 
</script> 
</head> 
<body bgcolor='#000000' text='#FFFFFF' vlink='#0000FF' onload='javascript:focus();'> 
<form name='mesform' action='chatlib.php' method='POST'> 
    <input type='hidden' name='password' value='<?=$password?>'> 
    <input type='text' size='20' name='message'> 
        <input type='submit' value='Send'> 
    <input type='button' name='Aggiorna' value='Aggiorna' onclick='javascript:aggiorna();'> 
    </font> 
</p> 
</form> 
</body> 
</html> 

<? 
if (empty($message)) { 
die(""); 
} 

$message = htmlspecialchars($message); 
$message = mysql_escape_string($message); 
$message = strip_tags($message); 

if (!isset($msg)) { 
$msg = "$nickname : $message"; } 

function php_private_chat($password,$msg) { 
$time = time(); 
mysql_db_query($db,"insert into chat_private (chat_date,chat_psw,chat_msg) values ($time,'$password','$msg')"); 
$time = $time - 86400; 
mysql_db_query($db,"delete from chat_private where chat_date <= $time"); 
} 

$msg = stripslashes($msg); 
$db = mysql_connect($hostname, $user, $password); 
php_private_chat($password,$msg); 
mysql_close(); 
?> 

// File chat_text.php 
<? 
echo "<meta http-equiv='Refresh' content='10'>"; 
// Db config 
$hostname = ""; 
$user = ""; 
$password = ""; 
?> 
<body bgcolor='#000000'> 
<?php 
$db = mysql_connect($hostname, $user, $password); 
$query = mysql_db_query($db,"select chat_msg from chat_private where chat_psw = '$password' and chat_date >= ($time-600) order by chat_date desc limit 0,20"); 
$num = mysql_num_rows($query); 
for ($i=0;$i<$num;$i++) { 
$array = mysql_fetch_array($query); 
echo $array[chat_msg]; 
} 
mysql_close(); 
?> 
</body> 

// File chat_read.php 
<HTML> 
<FRAMESET frameborder='no' border='0' ROWS='*,60'> 
<FRAME name='chatroom' SRC='chat_text.php?password=<? echo strtolower($password); ?>&time=<? echo time();?>'> 
<FRAME name='sendbar' SRC='chatlib.php?password=<? echo strtolower($password);?>'> 
</FRAMESET> 
</HTML>