简体   繁体   中英

Deleting File From Server After Session Ends

Can you please let me know how I can Delete a File from server After Session Ends when user close the browser or logs out.

As you can see from the following code, what I am trying to do is creating a file to store some query results on the server and keep it until session is running then eventually Delete it only after session ends.

<?
php session_start();

$info = "testFile.txt";
$ourFileHandle = fopen($info, 'w') or die("can't open file");
fclose($ourFileHandle);

// How to Delete a File When Session Ends on User closes the Browser or Log out
session_destroy();
$theFile = "info.txt";
$fh = fopen($theFile, 'w') or die("can't open file");
fclose($fh); 

Thanks

Short answer:

It is impossible, because php code is NOT executed when session ends

Long answer:

First, you need to understand how session works, when session actually ends?

When user closes browser - php session is not closed, when user logs out - php session is not closed.

PHP session is basically file which name is generated when user opens site for first time. This file name is stored as cookie in user's browser. For each request browser send this cookie, and when you call session_start in you code - this cookie is read from http request, file with such name is opened and loaded into memory. (note: session can be stored in memcache, database etc)

Next step - you can call session_destroy, which just deletes file (db record, etc). So next time browser call us with old cookie - we will read nothing, ie session is empty.

Session can expiry automatically, it is done internally by php (I suppose by internal thread or some other mechanism), to do this php stores last access (read) time of each session and when some session file is not touched for some time (configurable parameter) - this file deleted. Php will not call your code at this moment so it is impossible to delete something when session ended by php.

So, to solve your problem you can either:

  1. Store file content in session array instead of file system
  2. Re-invent the wheel and create your unique file name for each session in some folder, modify last access time every page hit, at the end of the script - delete all files which are not modified for long time

You can simply dete file in PHP using the unlink function,

unlink('test.html');

Make sure have given required permissions to php,

chmod('/var/www/html', 0750);

If user does log out,just delete file after doing the logout action. or you may run a small script to check if the session is valid(you can record a flag to check if a user is in process),and then delete the associated file.

You can write a custom session handler and configure that via session_set_save_handler() . Extend your handler from the default SessionHandler and override the destroy() and gc() methods. More info: http://php.net/manual/en/class.sessionhandler.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM