Newbie Problem: problem with HTTP authentication

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alsvider
Forum Newbie
Posts: 8
Joined: Fri Aug 05, 2005 10:38 am
Location: Quebec

Newbie Problem: problem with HTTP authentication

Post by alsvider »

I took the following code from PHP manual, but it doesn't work. Anyone has idea? I am running in Fedora Core 3, and my PHP is 4.3.11, Apache is 2.0.53-3.2.

Code: Select all

<?php
  if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Text to send if user hits Cancel button';
    exit;
  } else {
    echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
    echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
  }
?>

feyd | you're new, so I'll go light for now.. please use

Code: Select all

tags when posting php code.[/color]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

What is the error it is producing?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

make sure the code is placed before any output is sent to the browser.

see more...
alsvider
Forum Newbie
Posts: 8
Joined: Fri Aug 05, 2005 10:38 am
Location: Quebec

Post by alsvider »

I placed the above code in a separate file, that is all of the file. There is no error returned, just blank page.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

are you absolutely sure there's nothing before that code? no blank lines outside of the php tags, no echo's.. nothing?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Try commenting out the 401 header line. Your browser my be getting stuck on that and not outputing the text it should.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
alsvider
Forum Newbie
Posts: 8
Joined: Fri Aug 05, 2005 10:38 am
Location: Quebec

Post by alsvider »

I tried in another machine, it works. But I am thinking ie or firefox keep the authentication for a while, if login again immediately , the prompt windows will not come out. Am I right?
alsvider
Forum Newbie
Posts: 8
Joined: Fri Aug 05, 2005 10:38 am
Location: Quebec

Post by alsvider »

thx guys!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Horribly enough, IE handles this properly while FF doesn't (in my opinion). As long as a window is open, Firefox caches authentication credentials. So, as long as the current window is open and someone has authenticated, $_SERVER['PHP_AUTH_PW'] will exist. With the logic displayed on php.net, this will allow users to get in without needing to authenticate.

IE doesn't do this. It never stores PHP_AUTH_PW beyond the next page load. So, someone coming in after the fact won't be able to get in using someone else's credentials.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply