Beauford Posted September 3, 2008 Share Posted September 3, 2008 I am trying to open and grep certain content from a session file which is in the session_save_path() directory. I have managed to get the filenames into an array and can display the filenames, but no matter what I do I can't open and grep the files taking the filenames from the array. I want to find out the count of how may times a string is in the session file. If there are 100 files and only 6 match, I want to display 6. I tried something like the following (and several other variations), but not even sure I am on the right track. Any help is appreciated. Thanks $count = 0; for($i = 0; $i < $filnames; $i++) { if($file = fopen('grep -i "somestring" $filenames[$i],r')) { count++; } fclose($file); } Link to comment https://forums.phpfreaks.com/topic/122638-grepping-a-session-file-for-certain-content/ Share on other sites More sharing options...
discomatt Posted September 3, 2008 Share Posted September 3, 2008 This isn't really a solution, but you should use a custom session handler and move storage over to a database. You can then use Regex directly in the database query. Link to comment https://forums.phpfreaks.com/topic/122638-grepping-a-session-file-for-certain-content/#findComment-633234 Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 <?php //$filenames is your filename array: foreach ($filenames as $name) { $result = `grep -i "somestring" "$name"`; if (trim($result) != '') { $count++; } } ?> Try something like that. Link to comment https://forums.phpfreaks.com/topic/122638-grepping-a-session-file-for-certain-content/#findComment-633235 Share on other sites More sharing options...
Beauford Posted September 4, 2008 Author Share Posted September 4, 2008 Thanks, but one more problem. How do I specify the folder the file is in? $filenames contains the file name only, not where it is located. This is what I am using to get the file names in the array: $dir = opendir(session_save_path()); $filenames = array(); while (($file = readdir($dir)) !== false) { if ($file != "." and $file != "..") { array_push($filenames, $file); } } closedir($dir); sort($filenames); Thanks Link to comment https://forums.phpfreaks.com/topic/122638-grepping-a-session-file-for-certain-content/#findComment-633284 Share on other sites More sharing options...
DarkWater Posted September 4, 2008 Share Posted September 4, 2008 <?php //$filenames is your filename array: foreach ($filenames as $name) { $fullpath = session_save_path() . '/' . $name; $result = `grep -i "somestring" "$fullpath"`; if (trim($result) != '') { $count++; } } ?> I don't remember if session_save_path() returns a / on the end, but if it does, just remove the / from this code. Link to comment https://forums.phpfreaks.com/topic/122638-grepping-a-session-file-for-certain-content/#findComment-633287 Share on other sites More sharing options...
PFMaBiSmAd Posted September 4, 2008 Share Posted September 4, 2008 Because session data files are only removed when session garbage collection randomly runs, the existence of any session data file is not a true indicator of anything. What exactly are you trying to accomplish? Link to comment https://forums.phpfreaks.com/topic/122638-grepping-a-session-file-for-certain-content/#findComment-633291 Share on other sites More sharing options...
Beauford Posted September 4, 2008 Author Share Posted September 4, 2008 That 's what I thought about the path and had tried that earlier, but I keep getting the following error. I used your code, and even changed the slash to see if that would work. Warning: shell_exec() [function.shell-exec]: Unable to execute 'grep -i "loggedin|b:1" "d:\PHP 5.1\sessions/sess_ff0299f604aaaff262f4eef855211258"' in D:\Websites\Mysite\include\functions.php on line 219 This will eventually be on a linux/apache server but for now it is being built on Windows/IIS. What I am trying to do, and I know it's not an exact science, is to display a count of users who are logged in. Thanks Link to comment https://forums.phpfreaks.com/topic/122638-grepping-a-session-file-for-certain-content/#findComment-633322 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.