Jump to content

how many users online script help


1phpmeister

Recommended Posts

I looked for a script that shows the number of users online on the site i am making, and found this. I don't know if it is for just showing if the user is online on that one page like a profile page, or if it is for all users in the database. Also, when using the code, it shows "Users Online: 1" then when I reload the page, it says "2' and again, then "3" even though no users have logged in. Thank you.

 

the script

 

<?php /////////////////////////////////////////////////////////////////////////////////////////


////IS USER ONLINE SCRIPT PART////



$session=session_id();
$time=time();
$time_check=$time-600; //SET TIME 10 Minute

//CONNECT TO DB
//connect info here


// 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
/////////////////////////////////////////////////////////////////////////////////////////////



?> 

Link to comment
Share on other sites

It looks like it's generating a new session each time you run the script, so they are stored separately in the database. Then you are getting a count of the each individual sessions that are under 10 minutes old. Try changing your code to include:

$session=session_id();
if (session_id() == "") session_start();

Link to comment
Share on other sites

Thank you. It is still adding up the user on reload with the new code. Here is my database info as I don't really know if it is set up correctly.

 

 

CREATE TABLE `user_online` (
  `id_online` int(11) NOT NULL auto_increment,
  `session` varchar(20) NOT NULL,
  `time` int(20) NOT NULL,
  PRIMARY KEY  (`id_online`)
) ENGINE=MyISAM AUTO_INCREMENT=65 DEFAULT CHARSET=latin1 AUTO_INCREMENT=65 ;

 

Here is the new code.

 

<?php /////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////


////IS USER ONLINE SCRIPT PART////



$session=session_id();
if (session_id() == "") session_start();
$time=time();
$time_check=$time-600; //SET TIME 10 Minute

//CONNECT TO DB

//my connect info here

// 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
/////////////////////////////////////////////////////////////////////////////////////////////

		?>

Link to comment
Share on other sites

Thank you. I changed the code as you described but it still adds up the "user online" number on each new page load when I tests it.

 

Here is the code with the new change.

 

<?php 

////IS USER ONLINE SCRIPT PART////



if (session_id() == "") session_start();
$session=session_id();
$time=time();
$time_check=$time-600; //SET TIME 10 Minute

//CONNECT TO DB

//my db info here

// 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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.