Hi,
I have a really simple chat script running for my friends and I to discuss coursework. At the moment it allows users to enter the room even if they type the same name as someone who is already in there. What I would like is for it to compare the name entered with the users already in the room and return an error message telling them the name is already taken, and to enter a new one. The script I am using does not use a database so I am not entirely sure how to compare?
Here is the Login page:
<html>
<head>
<link href="style.css.php" rel="stylesheet" type="text/css">
</head>
<body class="main">
<table>
<tr>
<td class="maintitle">Secondar
y ICT Chat Room</td>
</tr>
</table>
<form method="get" action="
http://www.website.com/chat/index.php"
>
Please enter your nickname:
<input type="text" name="nickname">
<input type="submit" value="Go!">
</form>
</body>
</html>
Here is the chat script:
<?php ob_start();?>
<html>
<head>
<link href="style.css.php" rel="stylesheet" type="text/css">
</head>
<body class="main">
<?php
/*************************
******/
/*** Simple Simple Chat ***/
/*** v 1.0 ***/
/***
www.phpclub.site.ir ***/
/*************************
******/
// Config : [ You can change these options ]
$refresh = 6 ; // Page refresh time in seconds
$max = 20 ; // Max. saved messages
$remain = 1000 ; // Messages remaining time in seconds
$file = "msg.txt" ; // Message storing file
/*
You may need to make an empty file (use notepad) and name it "msg.txt"
($file), and send it to your server. Then CHMOD this file to 777 - e.g :
ftp> SITE CHMOD 777 msg.txt
*/
// End of config //
$user = $HTTP_GET_VARS['nickname']
;
$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*50 ) ) $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(stripslas
hes($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><me
ta 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['RE
QUEST_URI'
]}'>") ;
echo("<font color=green>Online Users: ") ;
foreach($users as $u) echo "<font color=#ff0000>".$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</t
itle><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>
<p>Secondary ICT Chat Room</p>
<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>
") ;
}
?>
Thank you in advance,
John
Start Free Trial