简体   繁体   English

会话结束后从服务器删除文件

[英]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 这是不可能的,因为会话结束时不会执行php代码

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会话未关闭,当用户注销时-php会话未关闭。

PHP session is basically file which name is generated when user opens site for first time. PHP会话基本上是用户首次打开站点时生成的名称的文件。 This file name is stored as cookie in user's browser. 该文件名作为cookie存储在用户的浏览器中。 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. 对于每个请求,浏览器都发送此cookie,并在代码中调用session_start时-从http请求中读取此cookie,将打开具有该名称的文件并将其加载到内存中。 (note: session can be stored in memcache, database etc) (注意:会话可以存储在内存缓存,数据库等中)

Next step - you can call session_destroy, which just deletes file (db record, etc). 下一步-您可以调用session_destroy,它只会删除文件(数据库记录等)。 So next time browser call us with old cookie - we will read nothing, ie session is empty. 因此,下次浏览器使用旧Cookie致电给我们时-我们将不会读取任何内容,即会话为空。

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在内部完成的(我想是通过内部线程或其他某种机制执行的),以使php存储每个会话的上次访问(读取)时间以及某个会话文件一段时间没有被触摸(可配置)参数)-此文件已删除。 Php will not call your code at this moment so it is impossible to delete something when session ended by php. php目前不会调用您的代码,因此在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函数在PHP中检测文件,

unlink('test.html');

Make sure have given required permissions to php, 确保已授予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() . 您可以编写一个自定义会话处理程序,并通过session_set_save_handler()配置。 Extend your handler from the default SessionHandler and override the destroy() and gc() methods. 从默认的SessionHandler扩展您的处理程序,并覆盖destroy()gc()方法。 More info: http://php.net/manual/en/class.sessionhandler.php 更多信息: http : //php.net/manual/zh/class.sessionhandler.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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