Php 如何在Kohana 3.1中设置受保护的静态文件
我正在使用启用ORM/Auth模块的Kohana3.1。我想服务的静态文件(文档,PDF等)只与角色A的人,在目录A_只 由于.htaccess文件只提供它直接找到的URL,而不将其交给index.php,因此我可以仅通过.htaccess来拒绝对A_中的所有访问,但是我如何在控制器函数中提供静态文件呢Php 如何在Kohana 3.1中设置受保护的静态文件,php,kohana,kohana-3,kohana-auth,Php,Kohana,Kohana 3,Kohana Auth,我正在使用启用ORM/Auth模块的Kohana3.1。我想服务的静态文件(文档,PDF等)只与角色A的人,在目录A_只 由于.htaccess文件只提供它直接找到的URL,而不将其交给index.php,因此我可以仅通过.htaccess来拒绝对A_中的所有访问,但是我如何在控制器函数中提供静态文件呢 我还可以在A_only目录中拥有一个.htaccess,该目录需要身份验证。但是,这将要求他们再次登录,即使我将其设置为在数据库中查找用户/密码。您需要告诉您的web服务器停止处理静态文件。最简
我还可以在A_only目录中拥有一个.htaccess,该目录需要身份验证。但是,这将要求他们再次登录,即使我将其设置为在数据库中查找用户/密码。您需要告诉您的web服务器停止处理静态文件。最简单的解决方案是将静态文件移到web目录之外,以便Apache无法找到它们;这将迫使该请求通过科哈纳 第二部分是创建一个控制器,为您处理权限和文件发送。Kohana用户指南中有一个很好的例子,可以帮助您解决一些问题:
剩下的就是样板 这个很好用,谢谢!我没有对Web服务器隐藏目录,而是放置了一个拒绝所有访问的.htaccess文件,然后使用路由将其定向到控制器。
// Get the file path from the request
$file = $this->request->param('file');
// Find the file extension
$ext = pathinfo($file, PATHINFO_EXTENSION);
// Remove the extension from the filename
$file = substr($file, 0, -(strlen($ext) + 1));
if ($file = Kohana::find_file('media/guide', $file, $ext))
{
// Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed
$this->response->check_cache(sha1($this->request->uri()).filemtime($file), $this->request);
// Send the file content as the response
$this->response->body(file_get_contents($file));
// Set the proper headers to allow caching
$this->response->headers('content-type', File::mime_by_ext($ext));
$this->response->headers('last-modified', date('r', filemtime($file)));
}
else
{
// Return a 404 status
$this->response->status(404);
}
if ($file = Kohana::find_file('media/guide', $file, $ext))