Is it possible to count users on certain page?

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
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Is it possible to count users on certain page?

Post by tomsace »

Hi,
I have the following code to count the total amount of users on my website:

Code: Select all

<? php
session_start();
$session=session_id();
$time=time();
$time_check=$time-600; //SET TIME 10 Minute
 
$host="localhost"; // Host name
$username="tomsace"; // Mysql username
$password=""; // Mysql password
$db_name="tomsace_games"; // Database name
$tbl_name="online_users"; // Table name
 
 
// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");
 
$sql="SELECT * FROM $tbl_name WHERE session='$session'";
$result=mysql_query($sql);
 
$count=mysql_num_rows($result);
 
if($count=="0"){
$sql1="INSERT INTO $tbl_name(session, time)VALUES('$session', '$time')";
$result1=mysql_query($sql1);
}
else {
"$sql2=UPDATE $tbl_name SET time='$time' WHERE session = '$session'";
$result2=mysql_query($sql2);
}
 
$sql3="SELECT * FROM $tbl_name";
$result3=mysql_query($sql3);
 
$count_user_online=mysql_num_rows($result3);
 
echo "<b>Total User Online:</b> $count_user_online "; 
 
// if over 10 minute, delete session 
$sql4="DELETE FROM $tbl_name WHERE time<$time_check";
$result4=mysql_query($sql4);
 
mysql_close();
 
?>
Which works great! But is it possible to use this method to count users on just one page for example?
I have searched google inside out but can't seem to find anything?
Last edited by Benjamin on Wed Apr 29, 2009 4:48 pm, edited 1 time in total.
Reason: Changed code type from text to php, removed password.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Is it possible to count users on certain page?

Post by Benjamin »

@tomsace, please start using [ code=php] tags instead of [ code=text]. Also, this is the 5th time you have posted your password. Stop it.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: Is it possible to count users on certain page?

Post by tomsace »

Hi, I noticed I didnt use the code=php and changed that myself, and sorry about the password I just copy and paste my code and totally forget about it...
Post Reply