Hi,

I'm sorry in advance if this is the wrong section.

I wanted to know is it possible to password protect some files in web page ?
For example, If someone wants to download file, browsers requests password(one for all visitors), idea is that some files and/or sections of web site is accessible to those who know the password..

Any advice , help much appreciated ! :)

Recommended Answers

All 16 Replies

try to code something like this -

function validate()
{
	var truePassword = 'test';
	var password = prompt('Enter the password','');
	alert(">>"+password+">>");
	
	if(password !='' && password !=null)
	{
		if(password != truePassword)
		{ 
			alert("You don't have download permissin for this");
		}
	}else
	{
		alert("Enter the password");
		
	}
}

where in code should I put this php ?

is it for single file or for section in web page ?

where in code should I put this php ?

is it for single file or for section in web page ?

Does it look like a PHP:'(
It may work against the single link which will prevent the download without entering the password

function validate()
{
	var truePassword = 'test';
	var password = prompt('Enter the password','');
	//alert(">>"+password+">>");
	
	if(password !='' && password !=null)
	{
		if(password != truePassword)
		{ 
			alert("You don't have download permissin for this");
			return false;
		}
	}else
	{
		alert("Enter the password");
		return false;
		
	}
}

It may still need some modification to it get working

whoops I'm sorry, don't know why I assumed that ..


if I have a .pdf file for example where should I put this code, or where in this code should I put this .pdf address ?

whoops I'm sorry, don't know why I assumed that ..


if I have a .pdf file for example where should I put this code, or where in this code should I put this .pdf address ?

call this function on click of the link, from where you download the pdf file.

call this function on click of the link, from where you download the pdf file.

This would still allow for downloading without password, just by looking at the page source.

Another way would be to put the pdf in a separate folder on the server, and password protect it (using your cPanel, htaccess, or whatever you're using).

Do I call function like this ?

<a href="faili/materiali/brosuras/maskesana.pdf" target="_blank" onclick="function name">

Do I call function like this ?

<a href="faili/materiali/brosuras/maskesana.pdf" target="_blank" onclick="function name">

yes, its doesn't seem to be fullproof, even if after some modification to it

It doesn't seem to work:

I wrapped code you provided in js tags (otherwise code showed as plain text on page)

Edited link like this <a href="faili/materiali/brosuras/maskesana.pdf" target="_blank" onclick="function validate">

<a href="faili/materiali/brosuras/maskesana.pdf" target="_blank" onclick="validate();">

Leave it, it wont work.Here is one more logic -
-redirect the user to another php page when he tries to download the pdf, adn check his username/password there.
-if username/password are right just get him back to the previous page or else directly exit or do whatever you wish.

There are 2 ways to securely do this. The first one is to make the download dir inaccessible without password (this is as said done in htaccess e.l. and not in you code). The other one is to put the file outside of you public_html-folder (http_root o.l. it might be called on a winserver) and use a script/program like php or C# to read the file and output it to the response-stream if the password is correct.

I found out about htaccess few hours ago. Now I know how can I password protect a single html page. which was one of the things I needed.

But how can I password protect single file or better yet <a href> tag ? I don't know much about php etc. so I wanted to ask in this forum for some help, maybe someone know some tutorials regarding this .. :)

I found out about htaccess few hours ago. Now I know how can I password protect a single html page. which was one of the things I needed.

But how can I password protect single file or better yet <a href> tag ? I don't know much about php etc. so I wanted to ask in this forum for some help, maybe someone know some tutorials regarding this .. :)

Password-protecting a single a-href doesn't make sense. What does is to password-protect the file that the a-href points to. And if you already know how to password-protect a html-file my guess is that it shouldn't be much of a difference in password-protecting a pdf-file or a img-file etc.

Thanks, everything works great, I don't know why, but somehow I imagined this would be much harder :p.

Got 1 question though:

when I clicked on .pdf link (without .htaccess) it opened in a new tab/window. Now when I have .htaccess, browser opens new tab and asks for authorization , which is all great, but if i press cancel I get Authorization Required error page. Is it possible to somehow get redirected back to the page from which I opened that .pdf instead of error page ?

I'm not shure about that. That's about server-settings, and not programming (.htaccess is too actually server-settings, but I'm not that familiar with it).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.