Jump to content

[SOLVED] PHP will not list session files?


mr_mind

Recommended Posts

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(); 
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.