当用户使用javascript登录时,我们如何强制删除缓存

时间:2010-09-27 14:25:16

标签: php javascript caching

有人能告诉我如何使用javascript删除浏览器缓存。我想要这个,因为我给用户,文件下载url('http://www.example.com/docs/doc1.xlsx')。此文件仅供该特定用户访问。

我正在检查htaccess重定向到其他操作,如果用户没有访问权限,则重定向到该特定文件URL,然后访问拒绝页面。

但问题是当有效用户下载该文件并从应用程序注销并复制到url上并在浏览器文件上输入时,无需访问服务器即可下载,这是由于浏览器中的缓存而发生的。

所以我想在用户退出系统时删除缓存。

非常欢迎替代解决方案。

4 个答案:

答案 0 :(得分:4)

简而言之,你不能(或者,至少,我从未见过这样做的方式)。

您需要通过发送正确的缓存清除标头在服务器端执行此操作。类似的东西:

Cache-Control: no-cache, must-revalidate, max-age=0

您可以使用(从the PHP documentation窃取示例):

header("Cache-Control: no-cache, must-revalidate, max-age=0");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

答案 1 :(得分:0)

这是不可能的。 Php仅适用于服务器端,由于安全问题,使用javascript无法正常工作。 ActiveX不是我猜的选择。

您可以做的是为每次必须重新加载的页面附加无缓存标头。

答案 2 :(得分:0)

我认为您应该在php中读取该文件并在下载之后下载,而不是直接访问该文件(如您提到的“http://www.example.com/docs/doc1.xlsx”)检查有效用户..

取自php.net

的示例
<?php
// downloading a file
$filename = $_GET['path'];

/**
* YOU CAN CHECK YOUR VALIDATIONS HERE.. 
*
*
*
*/


// fix for IE catching or PHP bug issue
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache

// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");

// use the Content-Disposition header to supply a recommended filename and
// force the browser to display the save dialog.
header("Content-Disposition: attachment; filename=".basename($filename).";");

/*
The Content-transfer-encoding header should be binary, since the file will be read
directly from the disk and the raw bytes passed to the downloading computer.
The Content-length header is useful to set for downloads. The browser will be able to
show a progress meter as a file downloads. The content-lenght can be determines by
filesize function returns the size of a file.
*/
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));

@readfile($filename);
exit(0);
?>

希望这会有所帮助.. 感谢...

答案 3 :(得分:0)

我已经解决了没有重大修改的问题。 首先,我按照人们的建议尝试设置标题。在重定向文件以进行下载之前,我尝试为缓存控制设置标头,但它根本不起作用。

所以我通过在.htaccess文件中设置标题找到简单的解决方案,该文件位于所有可下载文件所在的文件夹中。这意味着缓存控制头设置为下载文件响应。

但仍有一点需要考虑上述解决方案无效的原因。

还有一件事Pragma:“no-cache”不适用于IE。也就是说,在下载请求的文件时,由于“请求的站点不可用或无法找到”,它会出错。 所以我把它设置为Pragma:public。但我怀疑它是否安全。