使可下载的文件密码受网页保护 [英] Making a downloadable file password protected on webpage

查看:144
本文介绍了使可下载的文件密码受网页保护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个具有pdf下载选项的网页,但我希望它具有密码保护,即,如果有人单击该链接,则必须输入用户名和密码,并且如果他直接打开链接"www.example.com" /~folder_name/abc.pdf",然后服务器先要求输入密码,然后再允许下载

I want to make a webpage which has download option for a pdf, but i want it password protected i.e. if someone clicks on that link he has to enter username and password and if he directly open the link "www.example.com/~folder_name/abc.pdf" then server ask for password first and then allow to download

编辑:我希望用户在浏览器中查看文件,而不是强制下载 这是我的代码

I want user to view the file in browser, not to force it to download here is my code

<?php
    /* authentication script goes here*/
    $file = 'http://example.com/folder_name/abc.pdf';

    //header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: inline; filename=' . basename($file));
    header('Content-Transfer-Encoding: binary');
    //header('Expires: 0');
    //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    //header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    header('Accept-Ranges: bytes');
    @readfile($file);

?>

,但是此代码未在我的浏览器中打开pdf. 我不希望代码依赖于浏览器使用的pdf插件

but this code is not opening pdf in my browser. I don't want code to depend upon pdf plugin used by browser

推荐答案

您可以在设置了下载文件的Web文件夹中创建一个.htaccess文件,以便任何人都可以输入域之前,正确的用户名和密码才能进入.

You can make a .htaccess file in the web folder you have the download set up at so that before anyone can enter the domain, they have to enter the correct user and password to get in.

这是我自己设置的博客帖子本质上,您的.htaccess文件将如下所示:

Here's a blog post that I used when I set up my own but essentially your .htaccess file will look like this:

AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/file/directory-you-want-to-protect/.htpasswd
require valid-user

您还需要创建一个.htpasswd文件,您可以在其中放置用户名和密码.密码需要使用MD5哈希加密,但是您可以使用他在博客中链接到的生成器.希望这会有所帮助.

You also need to create a .htpasswd file where you can put a username and a password. The password needs to be encrypted with MD5 hash but you can use the generator he links to in his blog. Hope this helps.

这篇关于使可下载的文件密码受网页保护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆