authenticate using PHP_AUTH_USER & PHP_AUTH_PW

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rbragg
    New Member
    • Jul 2006
    • 6

    authenticate using PHP_AUTH_USER & PHP_AUTH_PW

    In my db, I have the fields user and pass with one record.

    With the following code, I get a continuous dialog box display. If I put in a bogus user/pass OR the correct user or pass, the dialog box pops up again.

    <?php

    //assume user is not authenticated
    $auth = false;

    $user = $_SERVER['PHP_AUTH_USER'];
    $pass = $_SERVER['PHP_AUTH_PW'];

    if ( isset($user) && isset($pass) )

    {
    //connect to db
    include 'db_connect.php ';

    //SQL query to find if this entered username/password is in the db
    $sql = "SELECT * FROM healthed_worksh op_admin WHERE
    user = '$PHP_AUTH_USER ' AND
    pass = '$PHP_AUTH_PW'" ;

    //put the SQL command and SQL instructions into variable
    $result = mysql_query($sq l) or die('Unable to connect.');


    //get number or rows in command; if more than 0, row is found
    $num_matches = mysql_num_rows( $result);

    if ($num_matches !=0)
    {
    //matching row found authenticates user
    $auth = true;
    }
    }

    if (!$auth)
    {
    header('WWW-Authenticate: Basic realm="Health Ed Presentation Admin"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'You must enter a valid username & password.';
    exit;
    }
    else
    {
    echo 'Success!';
    }
    ?>
  • rbragg
    New Member
    • Jul 2006
    • 6

    #2
    Nevermind. My problem was here:

    user = '$PHP_AUTH_USER ' AND
    pass = '$PHP_AUTH_PW'" ;

    This needed to be:

    user = '$user' AND
    pass = '$pass";

    ... since I had already defined these variables. ;)

    Comment

    Working...