tags:

views:

204

answers:

7

Hello i want to refresh my php every 3 sec this is what i have:

<?php
session_start();
include "../config.php";

$time = time();

$sql1 = "UPDATE login SET lastlogin = '" .$time. "' WHERE id = '" .$_SESSION['userid']. "'"; 
$res1 = mysql_query($sql1) or die(mysql_error());

$onlinetime = "10";  // 10 seconds

$sql3 =  "SELECT * FROM login ORDER BY lastlogin DESC"; 
$res3 = mysql_query($sql3);
    while($row3 = mysql_fetch_array($res3)){

if($row3['lastlogin'] + $onlinetime > time()){

echo '<b><font color="green">&#10004;</font></b> <u>'.$row3['name'].'</u><br />';

        }else{

echo "<b><font color='red'>X</font></b> <u>".$row3['name']."</u><br />";
        }
        }            

?>

This is not refreshing which is really irritating. It shows who's online at my chatbox. Someone help? Thanks.

+1  A: 

The best solution is to use ajax and poll a php script which checks for online users every X seconds.

I highly recommend jQuery for the ajax-part.

alexn
+1  A: 

Refreshing a browsers content is something the browser should do. Either use the Redirect-After HTML meta-tag (which will result in "flickering"), or use some Javascript to reload the site's content (see AJAX)

Martin Hohenberg
+7  A: 

I'd suggest using the <meta http-equiv="refresh" content="5" /> tag in the <head> section. This does not depend on JavaScript, and every major browser has this feature implemented.

Seb
A: 

You may adding some parens around lastlogin + onlinetime:

if($row3['lastlogin'] + $onlinetime > time()){

change to

if(($row3['lastlogin'] + $onlinetime) > time()){
Doug Hays
What does this have to do with refreshing the page?
Seb
I was wondering the same.
Homework
@seb; @joey; you right only answer on my question please. This stuff i could do with the validator...
YourComputerHelpZ
+4  A: 

I have no idea what you're trying to do. The code has no mention of a refresh in it.

You could put the following in the element of your HTML to force the client to refresh every three seconds.

<meta http-equiv="refresh" content="3" />

Ian

Ian P
A: 

You could use a javascript reload:

<META HTTP-EQUIV="Refresh" CONTENT="360; /">
Jakub
+1  A: 

You could refresh using PHP header or something by that means. It works for me every time.

<?php
$url = 'http://google.com/'; $timeout = 5; //Directed URL and timeout.
header('Refresh: ' . $timeout . ';url=' . $url);
?>
Homework
aI do not know for sure, but it seems to work fine... Thanks.
YourComputerHelpZ
it does not work, it is not refreshing. i did not change anything about this except the url.
YourComputerHelpZ
@YourComputerHelpZ Did you place this above everything else? Also, you must change the 5 to your directed time, as you said (3?) seconds...
Homework