user online code help needed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpuserfriendly
Forum Newbie
Posts: 1
Joined: Wed Feb 29, 2012 2:31 am

user online code help needed

Post 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
?>
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: user online code help needed

Post 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.
Post Reply