PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Nov 13, 2018 8:44 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Cron & Session Files...
PostPosted: Sat Nov 01, 2003 12:15 am 
Offline
Forum Contributor

Joined: Fri Sep 19, 2003 10:09 am
Posts: 145
Location: Earth
Cron is a wonderful tool :)
Use this about every hour (or half hour) to check for (and delete) session files over two hours old:

Syntax: [ Download ] [ Hide ]
<?php

   error_reporting  (E_ERROR | E_WARNING | E_PARSE);

   $session_files = array();

   $session_path = '/tmp';

   $dp = opendir($session_path);

   while (($contents = readdir($dp)) !== false) {

      if (!ereg("^\.",$contents) && ereg("^sess_",$contents)) {

         $session_files[] = $contents;

      }

   }

   if ( count($session_files) >= 1 ) {

      foreach ($session_files AS $gone_gone) {

      /* If the file was modified more than 2 hours ago, get rid of it.

         If not, it's probably pretty current so we'll keep it for a little while longer. */


         if ((filemtime( $session_path . '/' . $gone_gone) + (3600 * 2)) < time()) {

            unlink( $session_path . '/'.$gone_gone);

         }

      }

   }

?>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 7:26 am 
Offline
PHP Evangelist
User avatar

Joined: Tue Aug 13, 2002 3:35 pm
Posts: 3547
Location: Calgary, Canada
I recommend any cron file be blocked from access via a URL.

Here is an addition to that that would add more security for cron-scheduled php applications.

Syntax: [ Download ] [ Hide ]
<?php

if($REQUEST_URI) // URI = Uniform Resource Indicator (basically any requests via web browser)

{

    echo "You shouldn't be here!";   //error message

    exit; // so nothing more will load

}

   else

{

   error_reporting  (E_ERROR | E_WARNING | E_PARSE);

   $session_files = array();

   $session_path = '/tmp';

   $dp = opendir($session_path);

   while (($contents = readdir($dp)) !== false) {

      if (!ereg("^\.",$contents) && ereg("^sess_",$contents)) {

         $session_files[] = $contents;

      }

   }

   if ( count($session_files) >= 1 ) {

      foreach ($session_files AS $gone_gone) {

      /* If the file was modified more than 2 hours ago, get rid of it.

         If not, it's probably pretty current so we'll keep it for a little while longer. */


         if ((filemtime( $session_path . '/' . $gone_gone) + (3600 * 2)) < time()) {

            unlink( $session_path . '/'.$gone_gone);

         }

      }

   }

}

?>


Happy cron-ing


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 9:30 am 
Offline
Site Admin

Joined: Thu Apr 18, 2002 3:14 pm
Posts: 1767
Location: Montreal, CA
Or you could simply move the script out of the web directory, and into a directory not accessible from the outside. And simply use PHP via the command line.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group