How to programmatically access a password protected website?

Part of our site is protected by password protection such as .htaccess. When you try to access this area of ​​the website, the web browser opens a dialog box asking for your username and password.

I need to access this programmatically (e.g. via an ajax call). How does the server tell me that it needs a password and how can it be sent?

+7
ajax passwords web-services .htaccess
source share
4 answers
+11
source share

jQuery supports HTTP authentication using the ajax () method. Something like this should work:

jQuery.ajax({ type: "GET", url: "foo.php", username: "foo", password: "bar" }); 

Documentation on all jQuery.ajax () parameters can be found here: http://docs.jquery.com/Ajax/jQuery.ajax#options

+4
source share

In general, http: // user: pass@example.com

But there are some obvious security flaws.

A more complete solution would be to set a session variable after user authentication.

Make an AJAX script message that authenticates. If you are authenticated, use CURL to obtain results using a predefined authorized account.

This allows you to reuse the underlying apache auth, but does not allow you to write passwords in the DOM.

+3
source share

this, of course, is not ajax, but with the wget client you can use the -http-user and -http-password flags

+2
source share

All Articles