user1923631
user1923631

Reputation: 403

PHP - Access to a password protected page

I would just like to find a way in PHP (a link, a button, etc.) to give the user the possibility to open a (http-authentication-based) password protected page.

I already tried with a simple href containing the link to:

http://username:password@domain 

but it seems it does not work anymore in current browsers.

Is there a way to do this? It seems a very trivial problem, but actually I still have not found a way.

Thank you.

Upvotes: 1

Views: 1360

Answers (2)

aefxx
aefxx

Reputation: 25249

As you say, the username:password scheme won't work accross all browsers. What you can do - if you don't mind a script based solution - is to establish authentication via AJAX first, then present the user a normal link.

Idea outlined:

  1. A client side script establishes authentication via AJAX.
  2. Once this succeeds, you present the appropriate link to the user.
  3. The user's client automatically submits the credentials generated in step 1 once the user follows the link.

See this How-To for example.

Upvotes: 1

Omkar Khair
Omkar Khair

Reputation: 1384

Don't know why this should not work. Possible security issues that modern browsers are trying to avoid.

But to get this done, you could use

    <script>
function foo(){
window.location = "http://username:password@domain";
}
</script>

    <a onclick='foo()'>Click here</a>

Upvotes: 0

Related Questions