简体   繁体   English

覆盖htaccess的行为或通过URL访问文件的其他解决方案

[英]Either override behaviour of htaccess or a different solution for file access from URL

What it does? 它能做什么?

  1. All requests to PDF files are redirected to validation.php by htaccess . htaccess将所有对PDF文件的请求重定向到validation.php
  2. validation.php takes the log. validation.php记录日志。
  3. validation.php validates if user logged in or not. validation.php验证用户是否登录。
  4. If not logged in then kick user out. 如果未登录,则将用户踢出。 If logged in then show PDF file. 如果已登录,则显示PDF文件。

Problem: Obviously 4th step ( If logged in then show PDF file ) fails because of the behaviour of htaccess. 问题:显然第4步( 如果已登录,则显示PDF文件 )由于htaccess的行为而失败。

Question: How can I solve this issue? 问:如何解决此问题?

Thanks 谢谢

HTACCESS: .htaccess中:

RewriteEngine On
RewriteCond %{REQUEST_URI} \.(pdf)$ [NC]
RewriteRule ^(.*)$ /validate.php?filename=$1 [L]

validation.php: validation.php:

//STEP 1) Take a log
$file = 'log.txt';
$current = file_get_contents($file);
$current .= (isset($_GET['filename'])) ? $_GET['filename'] : '?';
$current .= " --- " . date('H:i:s') . "\n";
file_put_contents($file, $current);


//STEP 2) Authenticate login
session_start();

if (! isset($_SESSION['user']))
{
    session_write_close();
    header ('Location: /login.php');
    exit();
}
else
{
    //User should be eble to see the PDF file now.
}

In the //User should be eble to see the PDF file now. //User should be eble to see the PDF file now. step, instead of redirecting the user to the pdf file, simply output the file . 步骤,而不是将用户重定向到pdf文件, 只需输出文件 So something like: 所以像这样:

$file = $_GET['filename'];
$filename = basename($file);

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');

@readfile($file);

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

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