Jump to content

Grepping a session file for certain content.


Beauford

Recommended Posts

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

}

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

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

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?

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

 

 

Archived

This topic is now archived and is closed to further replies.

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