Log in

View Full Version : Mentor or somebody may be able to help...



UnderGROUND
22-04-2007, 08:38 AM
Thanks to Mentor, I now have a goo ajax reload. Anyway, I have a script which says how many users are online, and the ajax displays it and updates every 10 seconds. Anyway, I have it set to refresh every 10 seconds, and it deletes users from the database as soon as they're inactive, but I want it so it does it when they're not on the page. So say there are 3 users online: User 1 goes to the page, (it's a game, so they can't keep reloading the page), user 2 is also at the page and 3 is. So it displays 3 users online. Then 10 seconds later, it deletes from the database, so it says there is 0 users on. But they are still at the page and are actually on.

I want it so it says they're on when they're at the page, and for it to only delete them from the list/database when they actually leave the page. Heres the code:


<?
/**
*
* TG WHO'S ONLINE
* Copyright 2005 - 2006 (c) TOXIC GOBLIN
* http://www.toxicgoblin.com
*
*/

//Optional Database Connection Information
//**Uncomment the following 2 lines and edit the values if you do not already have an active database connection**
//
//$db = mysql_connect("localhost", "username", "password") or die("Could not connect");
//mysql_select_db("database_name");

include 'config.php';
//Fetch Time
$timestamp = time();
$timeout = $timestamp - 10;

//Insert User
$insert = mysql_query("INSERT INTO TG_whos_online (timestamp, ip, file) VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')") or die("Error in who's online insert query!");
//Delete Users
$delete = mysql_query("DELETE FROM TG_whos_online WHERE timestamp<$timeout") or die("Error in who's online delete query!");
//Fetch Users Online
$result = mysql_query("SELECT DISTINCT ip FROM TG_whos_online") or die("Error in who's online result query!");
$users = mysql_num_rows($result);

//Show Who's Online
if($users == 1) {
print("There is $users user online!\n");
} else {
print("There are $users users online!\n");
}
?>


As you can see it deleted them every 10 seconds, and updates the page so it says they're not there unless they refresh - then they're there again for 10 seconds. But I want it so they're on when they're at the page, and no on when they're not on it, but I want it to update instantly.

I hope you understand what I'm on about, anybody with good coding knowledge or in the know of a code, please help. :)

Thanks!

Edited by L&#181;ke (Forum Moderator): Thread Moved from Website Designing. Please post in the correct section next time, Thanks :).

Oni
22-04-2007, 08:41 AM
Ill m s n him

UnderGROUND
22-04-2007, 08:52 AM
Ok thanks Ozzie.

UnderGROUND
22-04-2007, 10:14 AM
*Can't edit*

$timeout = $timestamp - 10; is the part, I need it to be "when they close window" if you get what I mean. I want no timeout, only when they press X to the window (theres also a text link that says CLOSE).

R
22-04-2007, 10:19 AM
<?
/**
*
* TG WHO'S ONLINE
* Copyright 2005 - 2006 (c) TOXIC GOBLIN
* http://www.toxicgoblin.com
*
*/

//Optional Database Connection Information
//**Uncomment the following 2 lines and edit the values if you do not already have an active database connection**
//
//$db = mysql_connect("localhost", "username", "password") or die("Could not connect");
//mysql_select_db("database_name");

include 'config.php';
//Fetch Time
$timestamp = time();
$timeout = $timestamp - 3600;

//Insert User
$insert = mysql_query("INSERT INTO TG_whos_online (timestamp, ip, file) VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')") or die("Error in who's online insert query!");
//Delete Users
$delete = mysql_query("DELETE FROM TG_whos_online WHERE timestamp<$timeout") or die("Error in who's online delete query!");
//Fetch Users Online
$result = mysql_query("SELECT DISTINCT ip FROM TG_whos_online") or die("Error in who's online result query!");
$users = mysql_num_rows($result);

//Show Who's Online
if($users == 1) {
print("There is $users user online!\n");
} else {
print("There are $users users online!\n");
}
?>

UnderGROUND
22-04-2007, 10:50 AM
Yeah, Ryan, but if they leave it wont show it untill that amount of time. I want it so if somebody leaves, it goes down instantly, but not when somebody is on then it goes off and they're still there.

Mentor
22-04-2007, 12:24 PM
Well you cant really do it instantly via ajax, there will be some delay. Say if you dont mind the delay being 10 seconds, youd need the script to be accessed every 10 or maybe slightly less than 10 seconds to maintain the entery.

The easyest way to do this would just be change the ajax script to it connects to the online script every 10 seconds (or slightly less) ?

Also, are you sure "$timeout = $timestamp - 10;" shouldnt be "$timeout = $timestamp + 10;" ?

UnderGROUND
22-04-2007, 12:29 PM
Well you cant really do it instantly via ajax, there will be some delay. Say if you dont mind the delay being 10 seconds, youd need the script to be accessed every 10 or maybe slightly less than 10 seconds to maintain the entery.

The easyest way to do this would just be change the ajax script to it connects to the online script every 10 seconds (or slightly less) ?

Also, are you sure "$timeout = $timestamp - 10;" shouldnt be "$timeout = $timestamp + 10;" ?

Hi Mentor, I don't mind a delays, up to 1 min is fine!

How would I do that, could you provide a code? Thanks if you can!

It works with - 10, ill try + 10 thanks.

OK, I did + 10 and it displays 0 users, on - 10 it says 1. and works.
So, about this thing to connect to the script.

Mentor
22-04-2007, 01:21 PM
Well basicly the thing that connects to the scripts is the ajax script you requested erleyer, you just need to make it access the online script more often (aka when it uses the xmlhttp request to get the users online data)

Blob
22-04-2007, 01:29 PM
Cant you use javascript? Im sure theres a script which does like if me.close (vB i think lol) it does something, e.g.

Javascript code:

if(window.close) *

run a php script here which removes them from database

* someone make a script which does that for js.

UnderGROUND
22-04-2007, 01:45 PM
Well basicly the thing that connects to the scripts is the ajax script you requested erleyer, you just need to make it access the online script more often (aka when it uses the xmlhttp request to get the users online data)

hmm, I'm rubbish with ajax etc, do you know how to do it?


Cant you use javascript? Im sure theres a script which does like if me.close (vB i think lol) it does something, e.g.

Javascript code:

if(window.close) *

run a php script here which removes them from database



* someone make a script which does that for js.
Yeah, I'd use it, but I don't have the codes.

EDIT: I now have a user code that updates frequently woo! ^^ thanks

UnderGROUND
22-04-2007, 03:56 PM
CANT EDIT...

The script I found didn't work, so I still need help, heh.

UnderGROUND
26-04-2007, 04:11 PM
Sorry to bump, but anybody willing to help? =(

Want to hide these adverts? Register an account for free!