I have a chatbox via the meta refresh tag which is included on every page. Thing is, it's contributing to almost half of the loading time for loading a new page. Anyway to decrease this time by changing some codes (or the chat code itself)? I tried using Ajax but it still processed rather slowly.
chat history:
<?php
include('config.php');
$query = mysql_query("SELECT * FROM `chat` ORDER BY `id` DESC LIMIT 20");
while($row = mysql_fetch_assoc($query))
{
if($row['userID'] == 0) { $player = 'Guest'; }
else
{
$player_q = mysql_query("SELECT * FROM `users` WHERE `id`='" . $row['userID'] . "'");
$player_r = mysql_fetch_assoc($player_q);
$player = $player_r['callname'];
}echo '<font size=1><i><div align=right>' . date('M d, h:i:s', $row['time']) . '</div></font></i><div style="border-bottom: 1px solid black;"><font face=tahoma size=2><b>' . $player . ':</b> ' . $row['message'] . '</font></div>';
}
?>
<meta http-equiv="refresh" content="10">chat.php
<?php
include('config.php');
?>
<STYLE TYPE="text/css"> body{background-color:transparent}</STYLE>
<link rel="stylesheet" type="text/css" href="/chatstyle.css">
<?php
if(isset($_POST['form']))
{
$message = mysql_real_escape_string($_POST['message']);
$time = time();
if($_SESSION['id'] != "") { $userID = $_SESSION['id']; }
else { $userID = 0; }
mysql_query("INSERT INTO `chat` (`userID`, `time`, `message`) VALUES ('$userID', '$time', '$message')") or die(mysql_error());
}
mysql_real_escape_string($message);
echo '<iframe src=chat_hist.php frameborder=0 width=195 height=296px style=overflow:visible; align=center allowtransparency="true"><div id=Message align=left></iframe>
<form action="' . $_SERVER['PHP_SELF'] . '" method="POST">
<font style="padding-left:8px;" face=Tahoma size=2>Message: </font><input type=text name=message placeholder="Type a message?" size=25>
<div style="padding-right:5px;" align="right"><input type=submit value=Send name=form class=button /></div>
</form></div>';
?>header.php (where it's included):
<link rel="stylesheet" type="text/css" href="/style.css">
<link rel="stylesheet" type="text/css" href="/atooltip.css" media="screen" />
<link rel="shortcut icon" href="http://www.picpanda.com/images/lskmd1qf61gic1i60z4.ico" type="image/x-icon" />
<div id="container">
<div id="header" style="background: url('/test.png') no-repeat; height: 286px;"><ul id="topnav">
<li id="topnav-1"><a href="myprofile.php?id=<?php echo $userfinal; ?>" title="Home">Home</a></li>
<li id="topnav-2"><a href="roleplaying.php?rpid=1" title="City">City</a></li>
<li id="topnav-3"><a href="roleplaying.php?rpid=2" title="Coast">Coast</a></li>
<li id="topnav-4"><a href="roleplaying.php?rpid=3" title="Outlands">Outlands</a></li>
<li id="topnav-5"><a href="roleplaying.php?rpid=4" title="OOC">OOC</a></li>
<li id="topnav-6"><a href="news.php" title="News">News</a></li>
<li id="topnav-7"><a href="help.php" title="Help">Help</a></li>
</ul>
</div>
<div id="page">
<table align="center" width="949">
<tr valign="top"><td style="overflow:visible" align="center" width="220px">
<?php
if($loggedin == '0')
{
if(isset($_POST['submit']))
{
// Make sure all forms were filled out.
if((!isset($_POST['username'])) ||
(!isset($_POST['pass']))
|| ($_POST['username'] == '') || ($_POST['pass'] == ''))
die("Please fill out the form completely. <br><br>
<a href=index.php>Continue</a>");
// Get user's record from database
$player = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."' AND active IS NULL");
$player = mysql_fetch_assoc($player);
mysql_real_escape_string($username);
mysql_real_escape_string($password);
if($player['id'] == false)
die("Sorry, that user is not in our database or your account isn't activated.<br><br>
<a href=index.php>Back</a>");
else if($player['password'] != md5($_POST['pass']))
die("Wrong password!<br><br>
<a href=index.php>Back</a>");
$_SESSION['id'] = $player['id'];
$_SESSION['username'] = $player['username'];
$_SESSION['password'] = $player['password'];
$_SESSION['callname'] = $player['callname'];
$_SESSION['email'] = $player['email'];
$date = date("m/d/y");
$update = @mysql_query("UPDATE users SET lastlogin = '$date' WHERE id = '".$_SESSION['id']."'");
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=news.php">';
}
else
{
echo '<form action=index.php method=post><div style="padding-top:5px;" id=box><table>
<tr align=center>
<td width=200px>
<i><b>Sign in</b></i></td></tr>
<tr><td valign=middle>
<table><tr><td><input type=text name=username placeholder=Username size=25></td></tr></table>
</td></tr>
<tr>
<td valign=middle>
<table><tr><td><input type=password placeholder=Password name=pass size=25></td></tr></table>
</td>
</tr>
<tr><td align=right width=200px><input type=submit name=submit value=Login class=button><br /><br /><a href=#>Register!</a> or <a href=forgotpass.php>Forgot password?</a>
</form><br /><br /></td><tr><td align=left><iframe src="chat.php" width="100%" height="410px" align="left" frameborder="0" style="overflow:visible;"></iframe></tr></td></div>
</tr></table></div></center>';
}
}
else
{
$player_q = mysql_query("SELECT `callname` FROM `users` WHERE id = '".$_SESSION['id']."'");
$player_r = mysql_fetch_assoc($player_q);
$player = $player_r['callname'];
echo '<div style="padding-top:5px;" id=box><table align="left">
<i><b>Welcome Back!</b></i><br />Hey again, '.$player.'! <br /><br /><b>Gold:</b> 0<br /><b>Inbox Status:</b>';
$inbox = mysql_query("SELECT `status` FROM `inbox` WHERE to_mid = '".$_SESSION['id']."'");
$inbox = mysql_fetch_array($inbox);
{
if($inbox['status'] == 0){
echo 'NEW<br />'; }
else{ echo 'Old<br />'; }}
echo '<b>Recent Posts:</b> Old<br /><br /><center>2 users online<br /><br /></center><iframe src="chat.php" width="100%" height="410px" align="left" frameborder="0" allowtransparency="true"></iframe><br /><a href=logout.php>Logout?</a><br /><br /></center>';
echo '</div>
</table>';
}
?>
<td align="center">
<table width='100%'>
<tr>
<td>
<font color="695454">..</font>
</td>
</tr>
</table>