Hi i am in need of help with an ajax related problem on my site. I've coded an active users list which shows the members and guests online. I'm sure you've all seen active user lists before, like the one at the bottom of this forum for example but using ajax i created one which updates without you having to refresh the page. It all works fine except it is causing my site to freeze quite often. I have considered removing it but thn i would have to have a regular boring active users list so i was hoping somebody could help me speed it up and stop the freezing from happening. One thing i'm not sure about is whether it is the ajax or the PHP causing this. Anyway enough talk here's my code:
That function is run on the page load and then every 10 seconds after that.
And here is the PHP:
[PHP]
$time_formatted = date("H:i:s", time());
$result = mysql_query("DE LETE FROM active WHERE timeout < '$time_formatte d' OR time > '$time_formatte d'");
$result = mysql_query("DE LETE FROM guests WHERE timeout < '$time_formatte d' OR time > '$time_formatte d'");
$result = mysql_query("SE LECT * FROM active ORDER BY time DESC");
while($row = mysql_fetch_arr ay($result)){
switch($row['membertype']){
default:
$link_class = 'main_class';
$image = '/images/icons/user_online.png ';
break;
case"Head Admin":
$link_class = 'admin_class';
$image = '/images/icons/admin_online.pn g';
break;
case"Head Moderator":
$link_class = 'moderator_clas s';
$image = '/images/icons/moderator_onlin e.png';
break;
case"Admin":
$link_class = 'admin_class';
$image = '/images/icons/admin_online.pn g';
break;
case"Moderator" :
$link_class = 'moderator_clas s';
$image = '/images/icons/moderator_onlin e.png';
break;
case"Site tester":
$link_class = 'tester_class';
$image = '/images/icons/tester_online.p ng';
break;
} ?>
<table cellspacing="0" width="100%">
<tr>
<td width="100%">
<img src="<?php echo $image; ?>">
<span class="<?php echo $link_class ?>">
<a href="http://www.staticfire. co.uk/index.php?page= user_page&user= <?php echo $row['loginname']; ?>">
<?php echo $row['loginname']; ?>
</a>
</span>
</td>
<td width="50%" align="right">
<?php
$time = $row['time'];
$formatted_time = date("H:i",strt otime("$time")) ;
echo $formatted_time ;
?>
</td>
</tr>
</table>
<?php
}
//update active users
if(@$_SESSION['loggedin'] == 'true'){
$loginname = $_SESSION['loginname'];
$membertype = $_SESSION['membertype'];
$ip = $_SERVER["REMOTE_ADD R"];
$result = mysql_query("SE LECT * FROM active WHERE loginname='$log inname'");
if (mysql_num_rows ($result) < 1) {
$query="INSERT INTO active(
loginname,
time,
timeout,
ip,
membertype
)
VALUES(
'$loginname',
CURTIME(),
ADDTIME(CURTIME (), '00:10:00'),
'$ip',
'$membertype'
)";
$result = mysql_query($qu ery);
}else{
$result = mysql_query("UP DATE active SET time=CURTIME(), timeout=ADDTIME (CURTIME(), '00:10:00') WHERE loginname='$log inname'")
or die(mysql_error ());
}
}else{
$ip = $_SERVER["REMOTE_ADD R"];
$result = mysql_query("SE LECT * FROM guests WHERE ip='$ip'");
if (mysql_num_rows ($result) < 1) {
$query="INSERT INTO guests(
ip,
timeout
)
VALUES(
'$ip',
ADDTIME(CURTIME (), '00:10:00')
)";
$result = mysql_query($qu ery);
}else{
$result = mysql_query("UP DATE guests SET timeout=ADDTIME (CURTIME(), '00:10:00') WHERE ip='$ip'")
or die(mysql_error ());
}
}
$result = mysql_query("SE LECT * FROM guests");
$guest_no = mysql_num_rows( $result);
echo'<small>Gue sts: '.$guest_no.'</small>';
[/PHP]
If you can see anything in either the ajax or the PHP which may cause this freeze pleae let me know.
-Thanks
Code:
function Active (){ self.status = "Sending request" initialize(); request.onreadystatechange=function(){ if(request.readyState == 4){ var active = document.getElementById('active'); active.innerHTML = request.responseText } } var infoStr = "cmd=" + encodeURIComponent(escape('active')); var url = "ajaxrequest.php"; request.open('POST',url, true); request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); request.send(infoStr); var delay = 20000; var timerID = setInterval("Active()", delay); }
And here is the PHP:
[PHP]
$time_formatted = date("H:i:s", time());
$result = mysql_query("DE LETE FROM active WHERE timeout < '$time_formatte d' OR time > '$time_formatte d'");
$result = mysql_query("DE LETE FROM guests WHERE timeout < '$time_formatte d' OR time > '$time_formatte d'");
$result = mysql_query("SE LECT * FROM active ORDER BY time DESC");
while($row = mysql_fetch_arr ay($result)){
switch($row['membertype']){
default:
$link_class = 'main_class';
$image = '/images/icons/user_online.png ';
break;
case"Head Admin":
$link_class = 'admin_class';
$image = '/images/icons/admin_online.pn g';
break;
case"Head Moderator":
$link_class = 'moderator_clas s';
$image = '/images/icons/moderator_onlin e.png';
break;
case"Admin":
$link_class = 'admin_class';
$image = '/images/icons/admin_online.pn g';
break;
case"Moderator" :
$link_class = 'moderator_clas s';
$image = '/images/icons/moderator_onlin e.png';
break;
case"Site tester":
$link_class = 'tester_class';
$image = '/images/icons/tester_online.p ng';
break;
} ?>
<table cellspacing="0" width="100%">
<tr>
<td width="100%">
<img src="<?php echo $image; ?>">
<span class="<?php echo $link_class ?>">
<a href="http://www.staticfire. co.uk/index.php?page= user_page&user= <?php echo $row['loginname']; ?>">
<?php echo $row['loginname']; ?>
</a>
</span>
</td>
<td width="50%" align="right">
<?php
$time = $row['time'];
$formatted_time = date("H:i",strt otime("$time")) ;
echo $formatted_time ;
?>
</td>
</tr>
</table>
<?php
}
//update active users
if(@$_SESSION['loggedin'] == 'true'){
$loginname = $_SESSION['loginname'];
$membertype = $_SESSION['membertype'];
$ip = $_SERVER["REMOTE_ADD R"];
$result = mysql_query("SE LECT * FROM active WHERE loginname='$log inname'");
if (mysql_num_rows ($result) < 1) {
$query="INSERT INTO active(
loginname,
time,
timeout,
ip,
membertype
)
VALUES(
'$loginname',
CURTIME(),
ADDTIME(CURTIME (), '00:10:00'),
'$ip',
'$membertype'
)";
$result = mysql_query($qu ery);
}else{
$result = mysql_query("UP DATE active SET time=CURTIME(), timeout=ADDTIME (CURTIME(), '00:10:00') WHERE loginname='$log inname'")
or die(mysql_error ());
}
}else{
$ip = $_SERVER["REMOTE_ADD R"];
$result = mysql_query("SE LECT * FROM guests WHERE ip='$ip'");
if (mysql_num_rows ($result) < 1) {
$query="INSERT INTO guests(
ip,
timeout
)
VALUES(
'$ip',
ADDTIME(CURTIME (), '00:10:00')
)";
$result = mysql_query($qu ery);
}else{
$result = mysql_query("UP DATE guests SET timeout=ADDTIME (CURTIME(), '00:10:00') WHERE ip='$ip'")
or die(mysql_error ());
}
}
$result = mysql_query("SE LECT * FROM guests");
$guest_no = mysql_num_rows( $result);
echo'<small>Gue sts: '.$guest_no.'</small>';
[/PHP]
If you can see anything in either the ajax or the PHP which may cause this freeze pleae let me know.
-Thanks
Comment