Page 1 of 1

user online code help needed

Posted: Wed Feb 29, 2012 2:38 am
by phpuserfriendly
Hi, I am trying to get this code to work I found through searching. I am still unsure if this code lists ALL the users online, or if it just lists if the user is online for that page. for the last 10 minutes check. The problem I am getting when I am running this script is that it keeps creating new sessions and times in the database, then adds 1 to the "user online" on the page so that every time it loads, instead of saying user online 1. it says user online, 1, reload, 2, then reload, 3, etc, forever adding, please any help fixing this code would be greatly appreciated.

here is the code, a snippet of the page actually.

Code: Select all

<?php 
// Connect to server and select databse
mysql_connect("$host1", "$username1", "$password1")or die("cannot connect to server");
mysql_select_db("$db_name1")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name1 WHERE session='$session'";
$result=mysql_query($sql)or die(mysql_error());

$count=mysql_num_rows($result);

if($count=="0"){
$sql1="INSERT INTO $tbl_name1(session, time)VALUES('$session', '$time')";
$result1=mysql_query($sql1)or die(mysql_error());
}
else {
"$sql2=UPDATE $tbl_name1 SET time='$time' WHERE session = '$session'";
$result2=mysql_query($sql2)or die(mysql_error());
}

$sql3="SELECT * FROM $tbl_name1";
$result3=mysql_query($sql3)or die(mysql_error());

$count_user_online=mysql_num_rows($result3);

echo "User online : $count_user_online ";

// if over 10 minute, delete session
$sql4="DELETE FROM $tbl_name WHERE time<$time_check";
$result4=mysql_query($sql4);

mysql_close();

// Open multiple browser page for result
?>

Re: user online code help needed

Posted: Wed Feb 29, 2012 12:08 pm
by temidayo
Where is the value for $session set? The script is probably generating new $session value
for each page load. Each session equals each user.