mr_mind Posted December 12, 2007 Share Posted December 12, 2007 I am working on the problem of showing how many users are online. Although along the way i have com along a whole lot of hitches i have finally come up with something. It does not give me an error message like it was but it always returns that there are 0 files in the directory corresponding to the name specified. Here is my function maybe someone here can find my error. <?php function users_online() { $users_online_max_idle = 3; $users_online_handle = session_save_path(); $users_online_count = 0; $users_online_files = glob($users_online_handle . 'sess_*'); foreach($users_online_files as $users_online_file) { $script_time = time(); $file_time = fileatime($users_online_file); if(($script_time - $file_time) < $users_online_max_idle * 60) { $users_online_count = $users_online_count+1; } } return 'Online Users: ' . $users_online_count; } print users_online(); ?> Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 12, 2007 Share Posted December 12, 2007 echo out this: $users_online_handle = session_save_path(); it might be looking in the wrong directory, it depends on your host/settings might want to look in php info to see directories or echo out the files in this directory with other php directory functions other than glob() to see if it can find them you might be using incorrect syntax with the glob() function try a test run on similar search but in a directory where there are known files and you will know it should return that it found something that's all I can think of at the moment Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 13, 2007 Author Share Posted December 13, 2007 I know that the path is set properly because i set it myself and i tried doing it to all of the files (.. and . as well) and it worked fine so that isnt the problem Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 13, 2007 Share Posted December 13, 2007 I suggest you echo out the path/file that you are passing to the glob() function to make sure it is valid (I suspect a missing slash in it) - echo $users_online_handle . 'sess_*'; Your code worked for me when the path/file was an actual valid combination. If the path/file is correct, check your web server log for errors (a php script might not have permissions to access the session data files directly.) Also, a little old fashion debugging would help, add the following line right before the if() statement to see what the program is actually operating on - echo "File: $users_online_file, fileatime: $file_time, script time: $script_time, dif: ",$script_time - $file_time,"<br />"; Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 is sql not an option here? SQL makes a very powerful option here Just throwing out an idea <?php session_start(); //just need some session to generate an id and keep it current $_SESSION['User'] = "yes"; $id = session_id(); //Open SQL Connection $q = "Select count(*) from `Users_Online` Where UserID = '".$id."'"; $r = mysql_query($q) Or die(mysql_error()); if(Mysql_num_rows($q) >0){ $q = "Update `Users_Online` Set Last_action = '".$_SESSION['last_action']."'" where UserID = '".$id."'"; } else{ $q = "Insert into `Users_Online` (UserID,Last_action) VALUES('".$id."',NOW())"; } mysql_query($q) or die(mysql_Error()); $q = "Delete from `User_Online` Where Last_action = NOW()-3600"; $r = mysql_query($q) Or die(mysql_error()); ?> Then to output users online is super easy <?php //COnnect SQL $q = "Select count(*) from `Users_Online`"; $r = mysql_query($q) or die(mysql_Error()); echo "There are ".mysql_result($r,0)." Users Online right now."; ?> Just make sure that delete query reflects the time you wish to have the users stay active (which right now is 1 hour at 3600 seconds) Just a thought very simply Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 13, 2007 Author Share Posted December 13, 2007 I solved it without a database. it was a problem checking the file time. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 I know you solved it, but what happens when you get 1 million users on your super sweet site? Files needs to be locked to be altered, resulting in some massive load wait times Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 13, 2007 Author Share Posted December 13, 2007 yes but the same will hapen to a database being that a database is in all reality one big file Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 databases don't read like flat files it doesn't lock on writing Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 13, 2007 Author Share Posted December 13, 2007 yes but that doesn't matter when you have millions of users because the return time is still going to be a long time. the way i am now doing it you dont read any of the files. all you do is list which files are in the directory. Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 13, 2007 Share Posted December 13, 2007 Speed aside....... Once a user closes the browser the "session" in the user's eyes is lost, deleted, expired.. But physically in the the session that resides on the server, the cookie file, does it get deleted instantaneously as well? If so, this is certainly interesting as how the server knows when to delete the file or session. And if this does happen instantaneously, this certainly is an advantage over a db approach because with a DB you guess or give a user so many minutes before you deem him inactive if he doesnt refresh the page. I record active users within 2 minutes, if there is no activity from users in 2 minutes, then I deem them inactive. If a session file on the server gets deleted instantaneously, then surely it will be more precise in counting users that are active than a DB approach could. Although, a DB approach can yield more information than just # of users online, but also things like last visited..frequency..and users in the last so many minutes.. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 Precision or accuracy can not be measured with a system of this nature a system using Ajax would be the only usable solution for an accurate answer. This is because you are attempting to measure a continuous function (U(t)) with examining it at non continuous intervals (Page refreshes). The approach to the database is no different from that of the session because they both rely on the proof of a file existances. My issue with the session only one is you are reading everyone's data instead of using a Structured Query that the mysql version gives you. The accuracy of both is the same as sessions can be told to die after X minutes similar to how I told the database to delete all rows an hour after now. If the users revives they will be reentered, possibly with a new session id, but irregardless I dont' see any descripincies in the accuracy because there is non. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 13, 2007 Share Posted December 13, 2007 The Garbage Cleaner (GC) is set in the PHP.INI how often to run. This is when expired sessions get deleted. You can have a very short timer with a very aggressive GC ratio if you chose to. Speed aside....... Once a user closes the browser the "session" in the user's eyes is lost, deleted, expired.. But physically in the the session that resides on the server, the cookie file, does it get deleted instantaneously as well? If so, this is certainly interesting as how the server knows when to delete the file or session. And if this does happen instantaneously, this certainly is an advantage over a db approach because with a DB you guess or give a user so many minutes before you deem him inactive if he doesnt refresh the page. I record active users within 2 minutes, if there is no activity from users in 2 minutes, then I deem them inactive. If a session file on the server gets deleted instantaneously, then surely it will be more precise in counting users that are active than a DB approach could. Although, a DB approach can yield more information than just # of users online, but also things like last visited..frequency..and users in the last so many minutes.. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 which making it run very often will increase server load, runnning in a db will not affect server load as the load will be only 1/100 of your normal page load, and occur on page loads only Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 13, 2007 Author Share Posted December 13, 2007 When the user closes the browser the session file is deleted. And just to let you know a mysql database file is locked everytime it is accessed if you are running on a linux system so there is no advantage to using database approach and in fact using a session file approach will be more accdurate Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 14, 2007 Share Posted December 14, 2007 your not gonna win this battle doing it with sessions is way more resource intensive than sql peroid. Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 14, 2007 Author Share Posted December 14, 2007 But, in my opinion, it is more accurate, easier to do, and just plain a better way to do it Quote Link to comment 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.