tmk4php Posted October 17, 2008 Share Posted October 17, 2008 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 Link to comment https://forums.phpfreaks.com/topic/128850-delete-file-on-session-close/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 17, 2008 Share Posted October 17, 2008 Just store the xml content in the session and dynamically output the xml file using php. The session data file and the xml content will be automatically deleted when session garbage collection runs. Link to comment https://forums.phpfreaks.com/topic/128850-delete-file-on-session-close/#findComment-667968 Share on other sites More sharing options...
rhodesa Posted October 17, 2008 Share Posted October 17, 2008 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). Link to comment https://forums.phpfreaks.com/topic/128850-delete-file-on-session-close/#findComment-667973 Share on other sites More sharing options...
tmk4php Posted October 17, 2008 Author Share Posted October 17, 2008 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! Link to comment https://forums.phpfreaks.com/topic/128850-delete-file-on-session-close/#findComment-667979 Share on other sites More sharing options...
PFMaBiSmAd Posted October 17, 2008 Share Posted October 17, 2008 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. Link to comment https://forums.phpfreaks.com/topic/128850-delete-file-on-session-close/#findComment-667992 Share on other sites More sharing options...
rhodesa Posted October 17, 2008 Share Posted October 17, 2008 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; ?> Link to comment https://forums.phpfreaks.com/topic/128850-delete-file-on-session-close/#findComment-667996 Share on other sites More sharing options...
tmk4php Posted October 17, 2008 Author Share Posted October 17, 2008 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! Link to comment https://forums.phpfreaks.com/topic/128850-delete-file-on-session-close/#findComment-668007 Share on other sites More sharing options...
PFMaBiSmAd Posted October 17, 2008 Share Posted October 17, 2008 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. Link to comment https://forums.phpfreaks.com/topic/128850-delete-file-on-session-close/#findComment-668013 Share on other sites More sharing options...
rhodesa Posted October 17, 2008 Share Posted October 17, 2008 They can all use the same file, cus the same file will return different data depending on the session. Since what is returned is generated via PHP, you can return data specific to that user. Link to comment https://forums.phpfreaks.com/topic/128850-delete-file-on-session-close/#findComment-668022 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.