Here is a little challange, I have been working on this for hours and can not figure it out:
I found this great little free chat script that I am trying to incorporate into a website. The script unedited below works perfectly fine. However I am trying to make one small change but it does not seem to work. If you look at the script you will see that it displays the users ip address as an indentifier when people are chatting. I want to get rid of this and instead display an actual name. I set up a form which asks for the persons name (i named the variable $chatname) and the post then points to this script. You can see my changes to it below. Well the $chatname variable appears until the script refreshes twice and then the $chatname variable is gone.
UNEDITED SCRIPT:
<?php
$refresh = 6 ; // Page refresh time in seconds
$max = 20 ; // Max. saved messages
$remain = 400 ; // Messages remaining time in seconds
$file = "msg.txt" ; // Message storing file
$user = $HTTP_SERVER_VARS['REMOTE_ADDR'] ;
$f = file($file) ;
$users = array($user) ;
header ("Content-Type: text/html; charset=utf-8") ;
$fp = fopen($file, "w") ;
if (!$fp) { die ('Can not write to file .') ; }
for ($i=0; isset($f[$i])||$i==0; $i++)
{
$e=explode('||', $f[$i]) ;
if ($e[2]=="\r\n" && !in_array($e[0], $users) && ( time() - $e[1] < $refresh*2 ) ) $users[]=$e[0] ;
if ($i==0) { fputs($fp, $user."||".time()."||\r\n") ;
if ( isset($HTTP_POST_VARS['msg']) ) fputs ($fp, $user."||".time()."||".str_replace("||", "", str_replace("\n", "", htmlspecialchars(stripslashes($HTTP_POST_VARS['msg']))))."\r\n") ; }
if ( ($e[0]!=$user && $e[2]=="\r\n") || ( $i<$max && ($e[1]+$remain)>time() && $e[2]!="\r\n") ) fputs($fp, $f[$i]) ;
}
fclose($fp) ;
if (isset($HTTP_GET_VARS['f1']))
{
$f2 = file($file) ;
echo("<html><head><title></title><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head>") ;
echo("<body text=#0000cc><meta http-equiv='refresh' content='{$refresh}; url={$HTTP_SERVER_VARS['REQUEST_URI']}'>") ;
echo("<font color=#e22200>Online Users: ") ;
foreach($users as $u) echo "<font color=#ee0099>".$u."</font> - " ;
echo "</font><hr>" ;
for($i=0; isset($f2[$i]) && $i<$max; $i++) {
$e=explode("||", $f2[$i]) ;
if ($e[2]!="\r\n") echo "<font color=green>{$e[0]}</font> <font color=red>:</font> {$e[2]}<br>\r\n" ;
}
die("</body></html>") ;
}
else
{
die("
<html><head><title>Chat</title><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<script><!--
function msg(){ document.m.msg.focus(); } // --></script>
</head>
<body align='center' onLoad='msg()'> <center>
<iframe src='?f1=1' width='95%' height='85%'></iframe><br>
<table width='95%'><tr><td align='center' width='100%'>
<form action='' method='post' name='m'>
Message : <input name='msg' size=60> <input type='submit' name='send' value='Send'>
<br />
</form>
</td></tr></table>
</body></html>
") ;
}
?>
MY EDITED VERSION:
<?php
$refresh = 6 ; // Page refresh time in seconds
$max = 40 ; // Max. saved messages
$remain = 400 ; // Messages remaining time in seconds
$file = "msg.txt" ; // Message storing file
$user = $_POST['chatname'] ;
$f = file($file) ;
$users = array($user) ;
header ("Content-Type: text/html; charset=utf-8") ;
$fp = fopen($file, "w") ;
if (!$fp) { die ('Can not write to file .') ; }
for ($i=0; isset($f[$i])||$i==0; $i++)
{
$e=explode('||', $f[$i]) ;
if ($e[2]=="\r\n" && !in_array($e[0], $users) && ( time() - $e[1] < $refresh*2 ) ) $users[]=$e[0] ;
if ($i==0) { fputs($fp, $user."||".time()."||\r\n") ;
if ( isset( $HTTP_POST_VARS['msg']) ) fputs ($fp, $user."||".time()."||".str_replace("||", "", str_replace("\n", "", htmlspecialchars(stripslashes($HTTP_POST_VARS['msg']))))."\r\n") ; }
if ( ($e[0]!=$user && $e[2]=="\r\n") || ( $i<$max && ($e[1]+$remain)>time() && $e[2]!="\r\n") ) fputs($fp, $f[$i]) ;
}
fclose($fp) ;
if (isset($HTTP_GET_VARS['f1']))
{
$f2 = file($file) ;
echo("<html><head><title></title><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head>") ;
echo("<body text=#0000cc><meta http-equiv='refresh' content='{$refresh}; url={$HTTP_SERVER_VARS['REQUEST_URI']}'>") ;
echo("<font color=#e22200>Online Users: ") ;
foreach($users as $u) echo "<font color=#ee0099>".$u."</font> - " ;
echo "</font><hr>" ;
for($i=0; isset($f2[$i]) && $i<$max; $i++) {
$e=explode("||", $f2[$i]) ;
if ($e[2]!="\r\n") echo "<font color=green>{$e[0]}</font> <font color=red>:</font> {$e[2]}<br>\r\n" ;
}
die("</body></html>") ;
}
else
{
die("
<html><head><title>Chat</title><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<script><!--
function msg(){ document.m.msg.focus(); } // --></script>
</head>
<body align='center' onLoad='msg()'> <center>
<iframe src='?f1=1' width='95%' height='85%'></iframe><br>
<table width='95%'><tr><td align='center' width='100%'>
<form action='' method='post' name='m'>
Message : <input name='msg' size=60> <input type='submit' name='send' value='Send'>
<br />
<input type=hidden name=chatname value=$chatname>
</form>
</td></tr></table>
</body></html>
") ;
}
?>
P.S. One thing I noticed. If I run the script without my editing and look at the msg.txt file, I see this:
11.11.11.11||1085162505||
11.11.11.11||1085162490||
(the 11.11.11.11 ip address is just generic numbers i put in for this example)
Then when I run the script with my editing and look at the msg.txt file, I see this:
||1085162505||
jack||1085162490||
(Where jack was the name I entered for user name) It is missing jack for the first line. Why??
Ah, sorry again for the long post. Any help would be GREATLY appreciated.
Rulkster