Forum Moderators: coopster

Message Too Old, No Replies

How do I script basic authentication login?

I need to use an API call that is protected by 'basic authentication'

         

teaperson

2:00 pm on Apr 25, 2008 (gmt 0)

10+ Year Member



I'm using an API that is password-protected by basic authentication. When I load the URL in my browser, I get asked for my username and password. I'd like to be able to script the login in PHP. Any ideas how?

elitebomber

6:18 pm on Apr 25, 2008 (gmt 0)

10+ Year Member



That's a pretty loaded question. When I first decided to learn PHP a few years ago, I picked up a book and the first example they went through was user authentication.

Typically you'd have an html/php/jsp/whatever file with a form in it. The form has an action property. Set the action to doLogin.php or whatever file naming scheme you want to use. Inside of doLogin.php the control flow would look like:

Get post data using $_POST variables.

Query the database using the post data. You would probably want to hash the password information using MD5 and some added salt depending on the level of securtiy you need. Your MySQL query would look something like SELECT user_id FROM users WHERE username = '$username' AND password = '$password'

If the number of rows returned is 1, then it is a valid login and you can act accordingly, ie. register session variables and redirect.

If it's not then you can display an error message like invalid login and redirect.

I'd look more into PHP and MySQL.

teaperson

6:43 pm on Apr 25, 2008 (gmt 0)

10+ Year Member



Sorry, I didn't make my question clear. I don't need to authenticate anyone. I need to authenticate myself to a server I don't control, and which requires a username and password to be entered.