BizLab Posted May 21, 2010 Share Posted May 21, 2010 Here is my schnazzy users online "counter" that i worked up for my site. It will open the session save folder and count any unexpired (recently refreshed) sessions to give me an estimate of the number of users online. There is a small Regex in place to ensure that it only counts session files, in case anything else ends up in there. define("MAX_IDLE_TIME", 3); if ($handle = opendir(session_save_path())) { $count = 0; while (false !== ($file = readdir($handle))) { if(preg_match('/^sess_\w+/', $file)){ if(time()- fileatime(session_save_path() . '\\' . $file) < MAX_IDLE_TIME * 60){ $count++; } } } return $count; closedir($handle); } When running this script, i get the following error: Warning: opendir() [function.opendir]: open_basedir restriction in effect. File(/var/www/vhosts/domain.com/tmp) is not within the allowed path(s): (/var/www/vhosts/domain.com/httpdocs:/tmp) in /var/www/vhosts/domain.com/httpdocs/admin/office/index.php on line 107 Warning: opendir(/var/www/vhosts/domain.com/tmp) [function.opendir]: failed to open dir: Operation not permitted in /var/www/vhosts/domain.com/httpdocs/admin/office/index.php on line 107 Session Save path is (as found by running session_save_path()): /var/www/vhosts/domain.com/tmp // session save path I found a few mentions of bugs while searching around.. talking about leaving off the trailing '/' or not... but i tried both methods, and neither worked. Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/202493-open_basedir-restriction-error-logs-and-code-here-online-users-function/ Share on other sites More sharing options...
mcdsoftware Posted May 21, 2010 Share Posted May 21, 2010 Have you tried using glob instead of opendir? define("MAX_IDLE_TIME", 3); $sessions = glob(session_save_path() . '/sess_*'); $count = 0; foreach ($sessions as $session) { // do your expiry computation here... } Or you could always try changing the save path HTH Quote Link to comment https://forums.phpfreaks.com/topic/202493-open_basedir-restriction-error-logs-and-code-here-online-users-function/#findComment-1061572 Share on other sites More sharing options...
BizLab Posted May 21, 2010 Author Share Posted May 21, 2010 mcdsoftware - I like the way you are thinking, this seems like it would run faster than the original. At this point, i have 4 sessions in the file, and when running this code, i receive an empty array. As you can see, i even commented out the time exipry criteria to see if anything will display. still nothing define("MAX_IDLE_TIME", 3); // check to see if there was a result, if not - assign an empty array to the variable (glob(session_save_path()."/sess_*") ? $sessions = glob(session_save_path()."/sess_*") : $sessions = array()); $count = 0; print_r($sessions); if($sessions){ foreach ($sessions as $session) { // do your expiry computation here... //if(time()- fileatime($session) < MAX_IDLE_TIME * 60){ $count++; //} } } Quote Link to comment https://forums.phpfreaks.com/topic/202493-open_basedir-restriction-error-logs-and-code-here-online-users-function/#findComment-1061594 Share on other sites More sharing options...
mcdsoftware Posted May 21, 2010 Share Posted May 21, 2010 I just tested your code and it does list all my session files so it looks like it's working . Try to check if you have read access to the tmp directory. Maybe try fopen-ing a session file to see if there will be any errors. Quote Link to comment https://forums.phpfreaks.com/topic/202493-open_basedir-restriction-error-logs-and-code-here-online-users-function/#findComment-1061597 Share on other sites More sharing options...
BizLab Posted May 21, 2010 Author Share Posted May 21, 2010 So while trying to open the /tmp/ directory i end up with the first open_basedir error. I tried to alter the open_basedir setting at runtime with the ini_set() function to allow only this script to access the directory, but that didn't work either. The phpinfo shows the open_basedir to be set to: /var/www/vhosts/domain.com/httpdocs:/tmp i figure i need to back that down the path a little to the: /var/www/vhosts/domain.com/ or even /var/www/vhosts/domain.com/tmp/ Quote Link to comment https://forums.phpfreaks.com/topic/202493-open_basedir-restriction-error-logs-and-code-here-online-users-function/#findComment-1061610 Share on other sites More sharing options...
mcdsoftware Posted May 21, 2010 Share Posted May 21, 2010 Yep, if you can, give yourself permission to read/write on /var/www/vhosts/domain.com Quote Link to comment https://forums.phpfreaks.com/topic/202493-open_basedir-restriction-error-logs-and-code-here-online-users-function/#findComment-1061634 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.