1phpmeister Posted February 29, 2012 Share Posted February 29, 2012 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 ///////////////////////////////////////////////////////////////////////////////////////////// ?> Quote Link to comment https://forums.phpfreaks.com/topic/258002-how-many-users-online-script-help/ Share on other sites More sharing options...
batwimp Posted February 29, 2012 Share Posted February 29, 2012 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(); Quote Link to comment https://forums.phpfreaks.com/topic/258002-how-many-users-online-script-help/#findComment-1322472 Share on other sites More sharing options...
1phpmeister Posted February 29, 2012 Author Share Posted February 29, 2012 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 ///////////////////////////////////////////////////////////////////////////////////////////// ?> Quote Link to comment https://forums.phpfreaks.com/topic/258002-how-many-users-online-script-help/#findComment-1322480 Share on other sites More sharing options...
batwimp Posted February 29, 2012 Share Posted February 29, 2012 OK. Try switching the two lines to: if (session_id() == "") session_start(); $session=session_id(); Quote Link to comment https://forums.phpfreaks.com/topic/258002-how-many-users-online-script-help/#findComment-1322511 Share on other sites More sharing options...
1phpmeister Posted March 1, 2012 Author Share Posted March 1, 2012 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/258002-how-many-users-online-script-help/#findComment-1322546 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.