Jump to content

Delete file on session close


tmk4php

Recommended Posts

Hello,

I have a calculator program in PHP that starts a new session for each user and writes data to a unique xml file for that user.  Since a new file is created with each session, I would like the file to be deleted when the session ends so that our server doesn't fill up.  Is there a function that says "do this when the session ends"?  Is there any other way to accomplish this?

 

Many thanks in advance for any and all help  :D

Link to comment
https://forums.phpfreaks.com/topic/128850-delete-file-on-session-close/
Share on other sites

First, why not just store the data in the SESSION? Is there a specific NEED for it to be in a file?

 

Second, you could name the file the same as the persons SESSION ID, then check the folder where your session files are stored on a periodic basis, and compare the names. This assumes your sessions are stored in files and your PHP is properly configured to clean-up sessions.

 

Third, you could just check the date modified or date accessed time, and delete anything older then some interval (like 24 hours).

My app is a web-based calculator that displays outputs in a chart.  The calculations are done in PHP, and I set up a session variable to hold inputs.  The outputs are written to an xml file so that our charts can read the data (the charts are Flash, and are called with Javascript).  Right now, the xml file is given the name of the session id and is stored in a 'temp' folder.  I'm not sure at the moment how many potential users this calculator would have on a daily basis, so I want a way to clean up the temp folder without having to do it manually.

 

PFMaBiSmAd - what do you mean by dynamically output the xml file?  Is that what my program is doing currently?

 

Thank you again for the help!

Place the xml content into a session variable instead of writing it to a file -

<?php
session_start();
// create some test data in a session variable
$_SESSION['xml'] = <<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
?>

 

Dynamically output the xml  content. Put the following in some file, like dynxml.php -

 

<?php
session_start();
header('Content-Type: text/xml');
echo $_SESSION['xml'];
?>

 

Simply use the dynxml.php filename instead of whatever xxxxxx.xml file you are using now.

Or, generate the XML dynamically when it's called. So, have your Flash files point to a PHP file instead, let's call it xml_data.php. Then the contents of that file would be something like:

 

<?php
  // Set the Content Type
  header('Content-type: text/xml;');
  // Start the Session
  session_start();
  my $data = $_SESSION['data']; //This is an array of key/values with the data you want
  // Build the XML...I made this up, but you are probably doing something along these lines
  $sxe = new SimpleXMLElement('<graph />');
  $sxe->addAttribute('type', $data['type']);
  // Add rows
  foreach($data['rows'] as $row){
    $row = $sxe->addChild('row');
    foreach($row as $value)
      $row->addChild('value', $value);
  }
  // Print the XML
  echo $sxe->asXML();
  exit;
?>

 

I can't have every session use the same file name - this is how I started out - because if multiple users are writing to and reading from the same file at the same time with different data, there will be problems.

 

I will try storing the xml file in a session variable and passing it to Javascript (if that can be done). 

 

Thanks for the help!  :)

I am not sure what you mean in that post, but session data is unique to each visitor. When your web page/javascript/flash requests the .php file that dynamically outputs the xml, each different visitor will only receive his session data. One file name - dynamically outputs the unique data for each visitor.

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.