help me with this code plz

Hi, im new here and would like some help from you

i have this code which detects how many guests and members there are currently on my website. here is the code below

[code]<?php
include(‘config.php’);
include(‘functions.php’);
mysql_connect($server,$dbusername,$dbpassword);
mysql_select_db($db_name);

$uname = $_SESSION[‘user_name’];
if (!$uname)
{
$guest = 1;
} else
{
$guest = 0;
}
$ip = $_SERVER[“REMOTE_ADDR”];
$time = time();
$page = $_SERVER[“PHP_SELF”];
$query = “SELECT * FROM user_online WHERE ip = ‘$ip’”;
$result = mysql_query($query);
if (mysql_num_rows($result) == 1)
{
$query2 = “UPDATE user_online SET uname=’$uname’,time=’$time’,file=’$page’,guest=’$guest’ WHERE ip = ‘$ip’”;
$result2 = mysql_query($query2);
}
else
{
$query2 = “INSERT INTO user_online (uname, ip, time, file, guest) VALUES (’$uname’, ‘$ip’, ‘$time’, ‘$page’, ‘guest’)”;
$result2 = mysql_query($query2);
}

$query4 = “SELECT * FROM user_online WHERE guest = ‘1’”;
$result4 = mysql_query($query4);
$guestonline = mysql_num_rows($result4);
$query5 = “SELECT * FROM user_online WHERE guest = ‘0’”;
$result5 = mysql_query($query5);
$memonline = mysql_num_rows($result5);
echo "
Guests: $guestonline

Members: $memonline
";
?>[/code]

Now how do i proceed if i want the code to remove the guest from the database once he has stop viewing the website??

and how do i proceed if i want the code to remove the logged users once he has logged out?

I manage to make it this far but can’t seem to find a solution now… any help would me much appreciated!

http is a stateless protocoll so u can’t detect when a user is leaving. Take a look at the bottom of http://phphelp.com/forums there it sais “his data is based on users active over the past five minutes”. u have to do the same thing (but the time might differ)

to get that done just use:
[php]mysql_query(‘DELETE FROM user_online WHERE time<’.(time()-300)); // -300 means 300 sec = 5 min[/php]

ok the sql you gave me will actually remove the logged in user also. Am i wrong?

For the logged in user, i want them to be removed only when they logout… This can be done with the session i presume…

So how do i add this sql in order that it removes only the guest?

And basically, it should add the user in the database if he is still active, right? How to achieve this with this small code?

Sorry for taking your time…

this code was ment as guid. it can easyly be modyfied to only delete the guests:
[php]mysql_query(‘DELETE FROM user_online WHERE guest=1 ANDtime<’.(time()-300));[/php]
but this again wouldn’t be a good solution as the registered users don’t have to log out, they may just close the browser window. that’s why there has to be some kind of timeout for them as well. additionaly u may delete theyr entry in the same pease of code where u delete the session.

i know thats not a very good working system at all but there is nothing else u can do with just http and html.

yeah that sounds correct… thanks fot the tips man :lol:

Sponsor our Newsletter | Privacy Policy | Terms of Service