After a day of research I'm able to answer my own question, with all the details and nuances related to it.
Premise:
First of all I should address the confusion I had on the difference between using htaccess
or javascript
for authentication.
.htaccess
authentication is server side, based on Apache Web Server
.
javascript
authentication is client side so you cannot use it to prevent the user from accessing the files.
Quoting from .htaccess deny all except allow javascript access as well:
If javascript
can access the files, the client can too.
So the short answer to my question is that you cannot obtain the result shown here with client side scripting.
Solution using .htaccess
:
The link posted in the question - as @grandchild
pointed out - uses .htaccess
authentication. From the Apache Tutorial Web Page:
.htaccess
files (or "distributed configuration files") provide a way
to make configuration changes on a per-directory basis. A file,
containing one or more configuration directives, is placed in a
particular document directory, and the directives apply to that
directory, and all subdirectories thereof.
This means that using an .htaccess
configuration file, you can decide (on a directory basis) whether or not to grant clients the permission to access all the files in the directory containing the .htaccess
configuration file. Note: you could also use a finer level of detail, but explaining it becomes out of topic.
Below is a code snippet of the .htaccess
file to put in the directory on your server that you want to protect from direct access:
AuthUserFile /home/user_name/htaccess/.htpasswd <- path to password file
AuthGroupFile /dev/null
AuthName "This will appear in the prompt to client"
AuthType Basic
<Limit GET>
require valid-user
</Limit>
At the path indicated above you put the following .htpasswd
file:
test_user:$a23Tl82g5/yW9$fY84d0PkzV.WNF9w$%d-90
where each line represents a valid username:passwd
pair, and the password is generated using the MD5
algorithm. Link to a website you can use to obtain the above.
PHP Solution
The comment from @Herbert Van-Vliet
is correct but incomplete:
Create a div with a field for username and password, and hide that.
Then show it when the user clicks the download button. Your div can
also contain a button with more code etc, providing a link if the
credentials are good. But ALL CHECKING should be done server-side!
It's true that you can do what suggested but that will obtain fake security unless you make sure that the resources are not downloadable directly, and this is independent of the authentication procedure.
The correct way of approaching this solution is explained in Block direct access to a file over http but allow php script access.
Which is:
Put the files you want kept to yourself outside of the web root directory.
To still prevent HTTP requests to the files, add a .htaccess file
that blocks all communication.
For example:
Order deny,allow
Deny from all
- Setup a
PHP
script (not javascript
) that will authenticate the user. Your web server, and therefore your server side language, will still be able to read them because the directory's local permissions (regardless of the .htaccess
permissions) allow the web server to read and execute the files.